B. Preparation for International Women‘s Day(思维)Codeforces Round #544 (Div. 3)

原题链接: 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.

题意: 让你找出最多有多少个礼物可以被送出去,送出去的条件为礼物对的数量总和需要能整除 k k k

解题思路: 我们这个题很容易发现,我们并不在乎礼物的大小,我们只在乎礼物对 k k k取余的结果,若为 0 0 0,则能整除,则再与一个这样的组合即可送出去,若为 i i i,则我们需要找到一个礼物对 k k k取余为 k − i k-i ki的即可组合送出去。 故我们的目的所在其实就是寻找组合数量,我们可以先利用 m a p map map记录余数的个数,最后遍历即可解决,要注意我们并不是求对的数量,而是求礼物的数量。

AC代码

/*
*邮箱:unique_powerhouse@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>//POJ不支持

#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=a;i>=n;i--)

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;
}
posted @ 2022-03-26 16:49  unique_pursuit  阅读(34)  评论(0)    收藏  举报