map容器學習與使用 多校聯合 2013 ProblemA 小 Y做家教


map容器

#include <map>

map <int , int>mp;

map<int,int>::iterator it; 

begin() 返回指向map头部的迭代器

end() 返回指向map末尾的迭代器

erase() 删除一个元素

1iterator erase(iterator it); //通过一个条目对象删除

2iterator erase(iterator first, iterator last); //删除一个范围

3size_type erase(const Key& key); //通过关键字删除

find() 查找一个元素

insert() 插入元素

clear() 删除所有元素

count() 返回指定元素出现的次数

empty() 如果map为空则返回true

size() 返回map中元素的个数

swap() 交换两个map

map中的元素是自动按key升序排序,所以不能对mapsort函数:


1 map<Key,T>::iterator it;  

2 (*it).first;             // 指向key(of type Key)  

3 (*it).second;            // 映射的值(of type T)  

4 (*it);                   // the "element value" (of type pair<const Key,T>)   

也可以如下表达:


5 it->first;               // same as (*it).first   (the key value)  

6 it->second;              // same as (*it).second  (the mapped value)  



ProblemA 小 Y做家教
Time Limit: 2000 mSec Memory Limit : 65536 KB
Problem Description
小Y 经过千辛万苦,跋山涉水, 终于挤过了高考的独木桥, 进了大学, 作为他家所在的那条街
为数不多的大学生, 也算这条街上的名人了~这不寒假刚回家, 邻居家阿姨就过来找他让他
去辅导一下她家在上高三的MM, 小Y 顿时感到这是他实现人生价值的最好的方式了
(o(╯□╰)o).可是刚来MM 就给他一个难题了:
有这样的一个集合,集合由N 个不同的正整数组成,一旦集合中的两个数x,y 满足y = P*x,
那么就认为x,y 这两个数是互斥的,现在想知道给定的一个集合中两两之间不互斥的子集最
多能有多少个元素。小Y 在学校经过一个学期的各种游戏训练,对这种问题已经无能为力了,
只好去百度提问, 这不, 正巧你看到了, 你能帮帮他么( 这年头DS 找妹子不容易)~
Input
输入有多组数据,每组第一行给定两个数N 和P(1<=N<=10^5, 1<=P<=10^9)。接下来一行
包含N 个不同正整数ai(1<=ai<=10^9)。
Output
输出一行表示满足要求的子集的最大元素个数。
Sample
INPUT OUTPUT
输出一行表示满足要求的子集的最大元素个数。
Sample
INPUT
4 2
1 2 3 4

OUTPUT
3



#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <cassert>
#include <map>
using namespace std;

int N, K;
map<long long , bool>mp;

int
main()
{
    map<long long, bool>::iterator it;
    int sum;
    while(cin>>N>>K){
        sum=0;
        mp.clear();
        int c;
        for(int i=0; i<N; ++i){
            cin>>c;
            mp[c]=true;
        }
        int cnt;
        long long t;
        for(it=mp.begin(); it!=mp.end(); ++it){
            cnt=0;
            long long t=it->first;
            while(mp[t]){
                mp[t]=false;
                t = K*t;
                ++cnt;    
            }
            sum += cnt/2;
        }
        cout<<N-sum<<endl; 
    }
    return 0;   
}

你可能感兴趣的:(map容器學習與使用 多校聯合 2013 ProblemA 小 Y做家教)