Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) B. Divisiblity of Differences
http://codeforces.com/contest/876/problem/B
题意:
给出n个数,要求从里面选出k个数使得这k个数中任意两个的差能够被m整除,若不能则输出no。
思路:
差能够被m整除,其实就是对m取余的余数相同。那么就统计n个数的余数丢到一个map里面,最后判断是否有某个数的数量大于等于k。
代码:
1 #include <stdio.h> 2 #include <map> 3 #include <vector> 4 using namespace std; 5 6 map<int,int> mmp; 7 8 int a[100005]; 9 int b[100005]; 10 11 vector<int> v; 12 13 int main() 14 { 15 int n,k,m; 16 17 scanf("%d%d%d",&n,&k,&m); 18 19 for (int i = 0;i < n;i++) 20 { 21 int t; 22 23 scanf("%d",&t); 24 25 b[i] = t; 26 27 t %= m; 28 29 a[i] = t; 30 } 31 32 for (int i = 0;i < n;i++) mmp[a[i]]++; 33 34 map<int,int>::iterator it; 35 36 int ans = -1; 37 38 for (it = mmp.begin();it != mmp.end();++it) 39 { 40 if (it -> second >= k) 41 { 42 ans = it -> first; 43 break; 44 } 45 } 46 47 if (ans == -1) printf("No\n"); 48 else 49 { 50 printf("Yes\n"); 51 52 int cnt = 0; 53 54 for (int i = 0;i < n;i++) 55 { 56 if (b[i] % m == ans) 57 { 58 v.push_back(b[i]); 59 cnt++; 60 } 61 62 if (cnt >= k) break; 63 } 64 65 for (int i = 0;i < v.size();i++) 66 { 67 printf("%d%c",v[i],i == v.size() - 1 ? '\n' :' '); 68 } 69 } 70 71 return 0; 72 }
康复训练中~欢迎交流!