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。

代码:

 #include <stdio.h>
#include <map>
#include <vector>
using namespace std; map<int,int> mmp; int a[];
int b[]; vector<int> v; int main()
{
int n,k,m; scanf("%d%d%d",&n,&k,&m); for (int i = ;i < n;i++)
{
int t; scanf("%d",&t); b[i] = t; t %= m; a[i] = t;
} for (int i = ;i < n;i++) mmp[a[i]]++; map<int,int>::iterator it; int ans = -; for (it = mmp.begin();it != mmp.end();++it)
{
if (it -> second >= k)
{
ans = it -> first;
break;
}
} if (ans == -) printf("No\n");
else
{
printf("Yes\n"); int cnt = ; for (int i = ;i < n;i++)
{
if (b[i] % m == ans)
{
v.push_back(b[i]);
cnt++;
} if (cnt >= k) break;
} for (int i = ;i < v.size();i++)
{
printf("%d%c",v[i],i == v.size() - ? '\n' :' ');
}
} return ;
}
上一篇:vue中的组件化


下一篇:Unix环境高级编程-阻塞访问原理——等待队列