原题链接: https://codeforces.com/problemset/problem/1133/A
测试样例
Input
7 2
1 2 2 3 2 4 10
Output
6
Input
8 2
1 2 2 3 2 4 6 10
Output
8
Input
7 3
1 2 2 3 2 4 5
Output
4
Note
In the first example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes):
(2,3);
(5,6);
(1,4).
So the answer is 6.
In the second example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes):
(6,8);
(2,3);
(1,4);
(5,7).
So the answer is 8.
In the third example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes):
(1,2);
(6,7).
So the answer is 4.
题意: 让你找出最多有多少个礼物可以被送出去,送出去的条件为礼物对的数量总和需要能整除。
解题思路: 我们这个题很容易发现,我们并不在乎礼物的大小,我们只在乎礼物对取余的结果,若为
,则能整除,则再与一个这样的组合即可送出去,若为
,则我们需要找到一个礼物对
取余为
的即可组合送出去。 故我们的目的所在其实就是寻找组合数量,我们可以先利用
记录余数的个数,最后遍历即可解决,要注意我们并不是求对的数量,而是求礼物的数量。
AC代码
/*
*
*/
//POJ不支持
using namespace std;
const int inf=0x3f3f3f3f;//无穷大。
const int maxn=2e5+10;//限定值。
typedef long long ll;
int n,k;
int a[maxn];
int main(){
while(cin>>n>>k){
map<int,int> p;
rep(i,1,n){
cin>>a[i];
p[a[i]%k]++;
}
int ans=p[0]/2*2;
for(int i=1;i*2<=k;i++){
if(i==k-i){
ans+=p[i]/2*2;
}
else{
ans+=min(p[i],p[k-i])*2;
}
}
cout<<ans<<endl;
}
return 0;
}