1553 mutex, 1553 mutex
Time Limit: 1 s space limit: 128000 KB title level: GoldQuestionView running resultsDescriptionDescription
There is such a set, the number of elements in the set is determined by the given N, the elements of the set are N different positive integers, once the two numbers in the set x, if y satisfies y = P * x, the two numbers x and y are mutually exclusive. Now I want to know that the maximum subset of a given set satisfies the mutual exclusion conditions.
Input description
Input Description
The input has multiple groups of data. The first row of each group is given two numbers N and P (1 <= N <= 10 ^ 5, 1 <= P <= 10 ^ 9 ). The next line contains N different positive integers (1 <= ai <= 10 ^ 9 ).
Output description
Output Description
The output line indicates the maximum number of elements that meet the requirements of the subset.
Sample Input
Sample Input
4 2
1 2 3 4
Sample output
Sample Output
3
Data range and prompt
Data Size & HintThis question should be greedy, but it is more mysterious ..
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<map> 7 using namespace std; 8 void read(int &n) 9 {10 char c='+';int x=0;bool flag=0;11 while(c<'0'||c>'9')12 {c=getchar();if(c=='-')flag=1;}13 while(c>='0'&&c<='9')14 {x=x*10+(c-48);c=getchar();}15 flag==1?n=-x:n=x;16 }17 int n;18 int a[1000001];19 int comp(int a,int b)20 {21 return a<b;22 }23 //int hash[10000001];24 map<int,bool>hash;25 int main()26 {27 int p;28 read(n);29 read(p);30 for(int i=1;i<=n;i++)31 read(a[i]);32 int ans=0;33 sort(a+1,a+n+1,comp);34 for(int i=1;i<=n;i++)35 {36 if(hash[a[i]]==0)37 {38 hash[a[i]*p]=1;39 ans++;40 }41 }42 printf("%d",ans);43 return 0;44 }