Example #1
0
int main()
{
	inp2(n,k);
	for(i = 0; i<n ;i++)
		inp1(a[i]); 
	ma[a[n-1]] = n - 1;
 	ll fin_ans = n;
 	dp[n-1] = -1;
 	s.push(n-1);
 	for(i = n-2; i>=0 ; i--)
 	{
 		while((!s.empty())&&a[s.top()]<=a[i]) s.pop();
 		if(s.empty()) dp[i] = -1;
 		else dp[i] = s.top();
 		s.push(i);
 	}
 	ll least = LONG_MAX;
 	for(i = n-2 ; i>=0 ; i--)
	{
		ll f = fin_ans;
		ll temp = a[i] - k;
		if(temp > 0)
		{
			ll p = sqrt(temp);
			for(int j=1 ; j<=p; j++)
			{
				if(temp % j ==0)
				{
					ll first = j;
					ll second = temp/j;
					if((a[i]%first == k)&&(ma.find(first)!=ma.end()))
						least = min(least,ma[first]);
					if( (first != second)&&(a[i]%second == k) &&(ma.find(second)!=ma.end())&&(temp%second==0))
						least = min(least,ma[second]);
				} 
			}
			if(least!=LONG_MAX) fin_ans += least - i - 1; 
			else fin_ans += n - i - 1;
		 }
		else if(temp < 0)
			if(least == LONG_MAX)
				fin_ans += n - i - 1;
			else
				fin_ans += least - i - 1;
		else if(temp == 0)
		{
			if(dp[i] == -1)
				fin_ans += n - i - 1;
			else
			{
				fin_ans += dp[i] - i - 1;
				least = dp[i];
			}
		}
		ma[a[i]] = i;
		//printf("ans is %lld for %lld %lld\n",fin_ans - f,a[i],least );
	}
	printf("%lld\n",fin_ans );
	return 0;
}
Example #2
0
int calc_area( map<int, vector<int> > &P ){
	int len = 0;
	int lastX = -1;
	int res = 0;
	set<int> active;
	vector<int> V;
	set<int>::iterator act, prev;
	for ( map<int, vector<int> >::iterator it=P.begin(); it != P.end(); it++ ){
		if ( len > 0 ) res += len * ( it->first - lastX );
		// insert or remove points
		V = it->second;
		for ( int i=0; i < (int)V.size(); ++i ){
			if ( active.find(V[i]) != active.end() ) active.erase( V[i] );
			else active.insert( V[i] );
		}
		// recalculate len
		act = active.begin();
		if ( act != active.end() ) prev = act++;
		len = 0;
		for ( ; act != active.end(); ){
			len += ( *act - *prev );
			act++;
			if ( act != active.end() ){ 			
				prev = act++;
			}
		}
		lastX = it->first;
	}
	return res;
}
Example #3
0
 int get(int key) {
     if (cacheMap.find(key) == cacheMap.end()) {
         return -1; 
     }
     
     CacheNode * node = cacheMap[key]; 
     int result = node->val; 
     
     if (node != head) {
         node->prev->next = node->next; 
         
         if (node->next != NULL) {
             node->next->prev = node->prev; 
         }
         else {  // At tail
             tail = node->prev; 
         }
         
         node->prev = NULL; 
         node->next = head; 
         head->prev = node; 
         head = node; 
     }
     
     return result; 
 }
Example #4
0
void recognize(Mat frame) {
	Mat gray;
	vector< Rect_<int> > faces;

	cvtColor(frame, gray, COLOR_BGR2GRAY);
	faceDetector.detectMultiScale(gray, faces);
	LOGD("found %d faces", faces.size());

	for (int i=0; i<faces.size(); i++) {
		Rect faceRect = faces[i];
		Mat faceResized;
		if (requiresResize) {
			float ratio = (float) recognizerImageWidth / recognizerImageHeight;
			Rect faceInRatio = getTargetInRatio(faceRect, gray.cols, gray.rows, ratio);
			Mat face = gray(faceRect);
			cv::resize(face, faceResized, Size(recognizerImageWidth, recognizerImageHeight), 1.0, 1.0, INTER_CUBIC);
		} else {
			faceResized = gray(faceRect);
		}

		int prediction = recognizer->predict(faceResized);
		map<int, string>::iterator iterator = names.find(prediction);

		if (iterator == names.end()) {	// key not found
			LOGD("face %d: %d %d %d %d unrecognized", i, faceRect.x, faceRect.y, faceRect.width, faceRect.height);
		} else {
			string name = iterator->second;
			LOGD("face %d: %d %d %d %d %s", i, faceRect.x, faceRect.y, faceRect.width, faceRect.height, name.c_str());
			showRecognized(frame, faceRect, name);
		}
	}

}
Example #5
0
 void Dict(const string& dict_name, const map<string, int>& dict) {
   cout << dict_name << " : " << endl;
   for (auto itr = dict.begin(); itr != dict.end(); ++itr) {
     cout << itr->first << " : " << itr->second << endl;
   }
   cout << endl;
 };
Example #6
0
int solution(uint goal) {
    if (min_num.count(goal) == 1) {
        return (int) min_num.at(goal).size();
    } else {
        bool f = false;
        int result = INT32_MAX;
        multiset<uint> cur_set;
        int prev_sol;
        for (auto i = values.begin(); i != values.end(); i++) {
            if (goal - *i > goal) {
                continue;
            }
            prev_sol = solution(goal - *i);
            if (prev_sol == -1) {
                continue;
            }
            if (result > 1 + prev_sol) {
                f = true;
                result = 1 + prev_sol;
                cur_set = min_num.at(goal - *i);
                cur_set.insert(*i);
            }
        }
        min_num[goal] = cur_set;
        if (f) {
            return result;
        } else {
            return -1;
        }
    }
}
Example #7
0
void result::save_to_map()
{
    cout<<"Changing......"<<endl;
    int length;
    if(file_name[0] == 'f')//like f20140102_020.csv,first item has 5 characters.
        length = 5;
    else if(file_name[0] == 'o')//the first item of o20140102_020.csv has 10 characters.
        length = 10;
    while(input.getline(input_string,LINE_LENGTH))//get one line
    {
        for(int i = 0;i < length;i++)
        {
            temp[i] = input_string[i];//copy the first item to the char array
        }
        temp[length] = '\0';//add finish character to the last.
        //cout<<temp<<endl;
        string st(temp);//change char array to string
        pr = my_map.find(st);//find the item
        if(pr == my_map.end())//if can't find
          my_map.insert ( pair <string , int>  ( st, 1 ) );//then insert a new one to map
        else//if find
          my_map[st]++;//count plus
    }

}
Example #8
0
void DesignParser::parseECUs(xmlDocPtr doc, xmlNodePtr cur) {
//	FILE* fp = fopen("../Simulator/auto_generated_files/ecudec.hh", "w");
    char file_name[100];
    sprintf(file_name, "%secudec.hh", LOCATION);
	FILE* fp = fopen(file_name, "w");
	int cnt=0;
	cur = cur->xmlChildrenNode;
	while(cur != NULL) {
		char id[100], v[5];
		if((!xmlStrcmp(cur->name, (const xmlChar*)"ECU"))) {
			bool isVirtual = false;

			getCharProp(cur, "ID", id);
			getCharProp(cur, "Virtual", v);

			if(strncmp(v, "1", 1) == 0) isVirtual = true;
			if(isVirtual) {
				ecumap.insert(make_pair(id, ecumap.size()));
				fprintf(fp, "Resource *r%d = new Resource(%d, 1, 100, 100, \"%s\");\n", cnt, cnt, id);
				fprintf(fp, "resources.push_back(r%d);\n", cnt);
				fprintf(fp, "Scheduler *s%d = new Scheduler(1, 1, r%d);\n", cnt, cnt);
				cnt++;
			}
		}
		cur = cur->next;
	}
	fclose(fp);
}
tripoint vertical_move_destination( const map &m, const tripoint &t )
{
    if( !m.has_zlevels() ) {
        return tripoint_min;
    }

    constexpr int omtileszx = SEEX * 2;
    constexpr int omtileszy = SEEY * 2;
    real_coords rc( m.getabs( t.x, t.y ) );
    point omtile_align_start(
        m.getlocal( rc.begin_om_pos() )
    );

    const auto &pf_cache = m.get_pathfinding_cache_ref( t.z );
    for( int x = omtile_align_start.x; x < omtile_align_start.x + omtileszx; x++ ) {
        for( int y = omtile_align_start.y; y < omtile_align_start.y + omtileszy; y++ ) {
            if( pf_cache.special[x][y] & PF_UPDOWN ) {
                const tripoint p( x, y, t.z );
                if( m.has_flag( flag, p ) ) {
                    return p;
                }
            }
        }
    }

    return tripoint_min;
}
//------------------------------------------------
void sendUser(int connfd){
	string str=toString(accountUser.size());
	writeRecv(connfd, str.c_str(), str.length());cout<<str;
	
			
	for (std::map<Account, User>::iterator it=accountUser.begin(); it!=accountUser.end(); ++it){
		str=toString(3*2);// ID IP port
		writeRecv(connfd, str.c_str(), str.length());
		
		
		str="ID";
		writeRecv(connfd, str.c_str(), str.length());
		
		str=it->first.ID;
		writeRecv(connfd, str.c_str(), str.length());
		
		
		str="IP";
		writeRecv(connfd, str.c_str(), str.length());
		
		str=it->second.IP;
		writeRecv(connfd, str.c_str(), str.length());
		
		str="port";
		writeRecv(connfd, str.c_str(), str.length());

		str=toString(it->second.port);
		writeRecv(connfd, str.c_str(), str.length());
		
	}
}
Example #11
0
int DesignParser::getEcuIdx(char* ecuname) {
	int ret=-1;
	if(ecumap.find(ecuname) != ecumap.end()) {
		ret = ecumap[ecuname];
	}
	return ret;
}
int main(void) {
    int N, K, score, k, l, num(0);
    string id;
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> K;
        num += K;
        for (int j = 0; j < K; j++) {
            cin >> id >> score;
            local[i][score].insert(id);
            total[score].insert({id, 0});
            location[id] = i+1;
        }
        int l = k = 1;
        for (auto elem = local[i].rbegin(); elem != local[i].rend(); ++elem) {
            for (auto& it : (*elem).second) {
                total[(*elem).first][it] = l;
                k++;
            }
            l = k;
        }
    }
    cout << num << endl;
    l = k = 1;
    for (auto elem = total.rbegin(); elem != total.rend(); ++ elem) {
        for (auto& it : (*elem).second) {
            cout << it.first << " " << l << " " << location[it.first] << " " << it.second << endl;
            k++;
        }
        l = k;
    }
    return 0;
}
Example #13
0
int main(){
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif
    int t = II ; bool flag = true ;
    while(t--)
    {
        int n = II ;
        while(getchar()!='\n');
        if(!flag) cout<<endl; flag = false ;
        party.clear() , Cnt.clear() ;
        rep(i,n){
            string s1 , s2  ;
            getline(cin,s1);
            getline(cin,s2);
            party[s1] = s2 ;
        }
        int m = II ;
        int mx = 0 ;
        string MM ;
        while(getchar()!='\n');
        rep(i,m){
            string s1 ;
            getline(cin,s1);
            Cnt[s1]++;
            if( Cnt[s1] > mx ){
                mx  = Cnt[s1];
                MM = s1 ;
            }
        }
Example #14
0
int main() {
    int i, j, ans, len;
    unsigned long long temp;
    for(i = 1; i <= maxn; ++i) pbase[i] = pbase[i - 1] * base;
    while(~scanf("%d%d", &M, &L)) {
        scanf("%s", str);
        len = strlen(str);
        hashmap[len] = 0;
        for(i = len - 1; i >= 0; --i) hashmap[i] = hashmap[i + 1] * base + str[i];
        ans = 0;
        for(i = 0; i + M * L <= len && i < L; ++i) {
            ma.clear();
            for(j = i; j < i + M * L; j += L) {
                temp = hashmap[j] - hashmap[j + L] * pbase[L];
                ma[temp]++;
            }
            if(ma.size() == M) ++ans;
            for(j = i + M * L; j + L <= len; j += L) {
                temp = hashmap[j - M * L] - hashmap[j - M * L + L] * pbase[L];
                ma[temp]--;
                if(ma[temp] == 0) ma.erase(temp);
                temp = hashmap[j] - hashmap[j + L] * pbase[L];
                ma[temp]++;
                if(ma.size() == M) ++ans;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
Example #15
0
static bool checkDupe(struct MD5Context* ctx,
                      map<string,unsigned int>& hashes,
                      vector<unsigned int>& dupes, unsigned int pos)
{
  unsigned char digest[17];
  MD5Final(digest,ctx);
  digest[16] = 0;
  char hex[33];
  sprintf(hex, "%02X%02X%02X%02X%02X%02X%02X%02X"\
      "%02X%02X%02X%02X%02X%02X%02X%02X", digest[0], digest[1], digest[2],
      digest[3], digest[4], digest[5], digest[6], digest[7], digest[8],
      digest[9], digest[10], digest[11], digest[12], digest[13], digest[14],
      digest[15]);
  hex[32] = 0;
  map<string,unsigned int>::iterator it = hashes.find(hex);
  if (it != hashes.end())
  {
    dupes[pos] = it->second; 
    return true;
  }

  hashes.insert(make_pair(hex,pos));
  dupes[pos] = pos;

  return false;
}
int main() {
    string s;
    while(cin >> s) {
        while(ispunct(s[s.size()-1])) s.erase(s.size()-1);
        while(ispunct(s[0])) s.erase(0, 1);
        for(int i = 0; i < s.size(); ++i) s[i] = tolower(s[i]);
        for(int i = 0; i < s.size(); ++i) {
            if(ispunct(s[i])) {
                MP[s.substr(0, i)]++;
                s.erase(0, i + 1);
            }
        }
        MP[s]++;
    }
    for(map<string, int>::iterator it = MP.begin(); it != MP.end(); ++it) {
        if(it->second < cnt) continue;
        if(it->second > cnt) V.clear();
        cnt = it->second;
        V.push_back(it->first);
    }
    sort(V.begin(), V.end());
    cout << cnt << " occurrences" << endl;
    for(int i = 0; i < V.size(); ++i)
        cout << V[i] << endl;
}
Example #17
0
void load_dic(const char *dic_name)
{
    FILE *fp = fopen(dic_name,"r");
    char word[64];

    while(feof(fp) == 0)
    {
        fgets(word,64,fp);
        int len = strlen(word);
        if(word[len-1] == '\n') word[len-1] = '\0';

        unsigned int ch_hash = ch_word_hash(word);

        //cout << word <<" hash:"<<ch_hash<< endl;

        if(ch_dic.count(ch_hash))
        {
           // cout << "gg,there have collusion" << endl;

        }
        else
        {
           ch_dic.insert(pair<int,string>(ch_hash,word));

        }
    }

}
Example #18
0
    void operator()() {
#ifdef FAST_AND_FAT
        //! Performing feature transformation on a block results
        //! is about a factor of two speedup. This suggests moving
        //! feature storage from nodes to images. However, the
        //! change will make introduction of different node
        //! types (with different sized feature vectors) difficult.
        //! It also means that the metric functors would need to
        //! be passed a copy of the graph in addition to the nodes.
        for (set<unsigned>::const_iterator it = _imgIndxes.begin(); it != _imgIndxes.end(); ++it) {
            lock();
            map<unsigned, MatrixXf>::const_iterator ft = _imgFeatureData.find(*it);
            if (ft == _imgFeatureData.end()) {
                MatrixXf X(_Lt.cols(), _srcGraph[*it].numNodes());
                for (unsigned segId = 0; segId < _srcGraph[*it].numNodes(); segId++) {
                    X.col(segId) = _srcGraph[*it][segId].features;
                }
                ft = _imgFeatureData.insert(_imgFeatureData.end(), make_pair(*it, X));
            }
            unlock();

            const MatrixXf X = _Lt.triangularView<Eigen::Upper>() * ft->second;
            for (unsigned segId = 0; segId < _srcGraph[*it].numNodes(); segId++) {
                _negGraph[*it][segId].features = _posGraph[*it][segId].features = X.col(segId);
            }
        }
#else
        const TriangularView<MatrixXf, Eigen::Upper> Lt(_Lt);
        for (set<unsigned>::const_iterator it = _imgIndxes.begin(); it != _imgIndxes.end(); ++it) {
            for (unsigned segId = 0; segId < _srcGraph[*it].numNodes(); segId++) {
                _negGraph[*it][segId].features = _posGraph[*it][segId].features = Lt * _srcGraph[*it][segId].features;
            }
        }
#endif
    }
Example #19
0
void csstidy::merge_4value_shorthands(string media, string selector)
{
	for(map< string, vector<string> >::iterator i = shorthands.begin(); i != shorthands.end(); ++i )
	{
		string temp;

		if(css[media][selector].has(i->second[0]) && css[media][selector].has(i->second[1])
		&& css[media][selector].has(i->second[2]) && css[media][selector].has(i->second[3]))
		{
			string important = "";
			for(int j = 0; j < 4; ++j)
			{
				string val = css[media][selector][i->second[j]];
				if(is_important(val))
				{
					important = "!important";
					temp += gvw_important(val)+ " ";
				}
				else
				{
					temp += val + " ";
				}
				css[media][selector].erase(i->second[j]);
			}
			add(media, selector, i->first, shorthand(trim(temp + important)));		
		}
	}
} 
Example #20
0
main()
{
    int n,Q ; scanf("%d%d",&n,&Q) ;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]) ;
    for(int i=1;i<=n;i++)
    {
        auto it=mp.find(a[i]) ;
        le[i]= it==mp.end() ? -1 : it->second ;
        mp[a[i]]=i ;
    }

    build(1,n,1) ;
    for(int i=1;i<=Q;i++)
        scanf("%d%d",&q[i].l,&q[i].r) , q[i].id=i ;
    sort(q+1,q+Q+1) ;
    for(int i=1,j=1;i<=Q;i++)
    {
        while(j<=q[i].r)
        {
            if(le[j]!=-1) modify(1,le[j],1,n,1,j-le[j]) ;
            j++ ;
        }
        ans[q[i].id]=query(1,n,1,q[i].l) ;
    }
    for(int i=1;i<=Q;i++) printf("%d\n",ans[i]==INF?-1:ans[i]) ;
}
ll exploration(map<ll,qualifiedVM> omega)                  //This is not working properly
{
	ll probabDistribution[2][MAX];
	for(int i=0;i<2;i++)
	{
		for(int j=0;j<MAX;j++)
		{
			probabDistribution[i][j]=0;
		}
	}
	int i=0;
	ll sum=0;
	map<ll,qualifiedVM>:: iterator it=omega.begin();
	probabDistribution[0][i]=ceil((it->second.probability)*100);
	sum=probabDistribution[0][i];
	probabDistribution[1][i++]=(it->first);

	it++;
	for(it;it!=omega.end();it++)
	{
		probabDistribution[0][i]=probabDistribution[0][i-1]+ceil((it->second.probability)*100);
		sum=probabDistribution[0][i];
		probabDistribution[1][i++]=(it->first);
	}
	ll num=rand()%(sum+1);
	for(register int j=0;j<i;j++)
	{
		if(num<=probabDistribution[0][j])
		{
			return probabDistribution[1][j];
		}
	}
}
//ζ‹“ζ‰‘ζŽ’εΊ
vector<int> TopoSort(map<int, VNode>  index_v , map<int, vector<Edge> >  table)
{
	//init s
	stack<int> s;
	vector<int> result;
	for (int i = 0; i < index_v.size(); ++i)
	{
		if (index_v[i].indegree == 0)
		{
			s.push(index_v[i].nodeid);
		}
	}//init complete

	while (!s.empty())
	{
	if (s.empty())
		return result;
	int delete_nameid = s.top();
	for (int i = 0; i < table[delete_nameid].size(); ++i)
	{
		int head_id = table[delete_nameid][i].head;
		if (--index_v[head_id].indegree == 0)
			s.push(head_id);
	}
	result.push_back(delete_nameid);
	index_v.erase(delete_nameid);
	table.erase(delete_nameid);

	}//while

	return result;
}
Example #23
0
void ispisiMap(map<int, int> M)
{
    map<int, int>::iterator it;

    for(it = M.begin(); it != M.end(); it++)
        cout << it->first << " " << it->second << " ";
}
Example #24
0
int main(int argc, char** argv) {
	ros::init(argc, argv, "proj4_visualizer");
	ros::NodeHandle n;
	ros::Rate r(1);
	
	ros::Subscriber costmapSub = n.subscribe("costmap_visualizer", 100, &subscribeCostmapCallback);
	ros::Subscriber pathSub    = n.subscribe("path_visualizer", 100, &subscribePathCallback);	
	ros::Publisher costmapPub  = n.advertise<visualization_msgs::Marker>("costmap_markers", 10);
	ros::Publisher pathPub     = n.advertise<visualization_msgs::Marker>("path_markers", 10);

	n.getParam("proj4_visualizer", grid_spacing);

	while(ros::ok()) {
		map<string, visualization_msgs::Marker>::iterator end = markers.end();
		for(map<string, visualization_msgs::Marker>::iterator it = markers.begin(); it != end; it++) {
			it->second.header.stamp = ros::Time::now();
			costmapPub.publish(it->second);
		}
		if(received_path) {
			path.header.stamp = ros::Time::now();
			pathPub.publish(path);
		}
		ros::spinOnce();
		r.sleep();
	}
	return 0;
}
void backtracking(vi s,vi steps, int d){
	if(vist.find(s) != vist.end() || d > limit){
//		printf("return d = %d limit = %d\n", d, limit);
		return;
	}
	if(done || isDone(s)){
		if(!done){
//			printf("found!\n");
			ans = steps;
		}
		done = true;

		return;
	}
	vist[s] = 1;

	int cost = d + H(s);
	printf("H(s) = %d d = %d \n", H(s), d);
	printf("best = %d\tcost = %d\n", best,cost);
	debug(s);
	if(cost <= best){
		best = cost;
		for (int i = 0; i < 4; ++i) {
			vi t = s;
			if(move(t, i)){
				printf("moving %c\n", toc(i));
				steps.push_back(i);
				backtracking(t,steps,d + 1);
				steps.pop_back();
			}
		}
	}
}
Example #26
0
int main() {
    scanf("%d", &t);
    while (t --) {
        mp.clear();
        scanf("%d", &n);
        forn (i, n) {
            scanf("%s%d", s, &w);
            string c(s);
            auto it = mp.find(c);
            if (it == mp.end()) {
                mp[c] = {w, 0};
            } else {
                int x = it->py.px;
                int y = it->py.py;
                if (x < w) {
                    y = x;
                    x = w;
                } else if (y < w) {
                    y = w;
                }
                mp[c] = {x, y};
            }
        }
        int ans = 0;
        iter (it, mp) {
            ans += it->py.px + it->py.py;
        }
Example #27
0
    int numDecodings(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
	if( s.length() < 1)
		return 0;
	if(caches.find(s) != caches.end())
	{
		return caches[s];
	}
	if(s[0] == '0')
		return caches[s]= 0;

	if(s.length() > 1)
	{
		if( s[0] == '1' || ( s[0] == '2' && s[1] < '7'))
		{
			int sum1 =  numDecodings(s.substr(1));
			int sum2 = 1;
			if( s.length() > 2)
			{
				sum2 = numDecodings(s.substr(2));
			}
			return sum1 + sum2;
		}
		else
		{
			return caches[s] = numDecodings(s.substr(1));
		}
	}
	return caches[s] = 1;        
    }
Example #28
0
int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    int t; cin >> t;
    while(t--) {
        cin >> n;
        for(int i = 1; i <= n; ++i) {
            cin >> a[i];
            pre[i] = -1;
            nxt[i] = n + 1;
        }
        mp.clear();
        for(int i = 1; i <= n; ++i) {
            if(mp.count(a[i])) {
                pre[i] = mp[a[i]];
                nxt[pre[i]] = i;
            }
            mp[a[i]] = i;
        }

        if(dfs(1, n)) cout << "non-boring\n";
        else cout << "boring\n";
    }
    return 0;
}
 bool checkNearby(map<int, int> &mm, int num1, int diff) {
     auto it = mm.find(num1);
     if (it->second > 1) {
         return true;
     }
     
     int num2;
     if (it != mm.begin()) {
         --it;
         num2 = it->first;
         ++it;
         if (abs((int64_t)num1 - num2) <= diff) {
             return true;
         }
     }
     if (next(it) != mm.end()) {
         ++it;
         num2 = it->first;
         --it;
         if (abs((int64_t)num1 - num2) <= diff) {
             return true;
         }
     }
     
     return false;
 }
Example #30
0
int main() {
  scanf("%d%d", &n, &k);
  for(int i = 1; i <= n; i++) {
    scanf("%d", &v[i]);
    fa[i] = i;
    pos[v[i]] = i;
  }
  for(int i = 1; i <= n; i++) {
    if(v[i] % k == 0) {
      int div = v[i] / k;
      if(pos.count(div)) {
	union_set(pos[div], i);
      }
    }
  }
  for(int i = 1; i <= n; i++) {
    int p = find_set(i);
    if(cnt.count(p))
      cnt[p]++;
    else
      cnt[p] = 1;
  }
  int ans = 0;
  for(map<int, int>::iterator itr = cnt.begin(); itr != cnt.end(); itr++) {
    ans += (itr->second + 1) / 2;
  }
  printf("%d\n", ans);
  return 0;
}