Codeforces Round 544(Div.3) B. Preparation for International Women's Day解题报告

题目链接

B. Preparation for International Women's Day
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

International Women’s Day is coming soon! Polycarp is preparing for the holiday.

There are n n n candy boxes in the shop for sale. The i i i-th box contains d i d_i di candies.

Polycarp wants to prepare the maximum number of gifts for k k k girls. Each gift will consist of exactly two boxes. The girls should be able to share each gift equally, so the total amount of candies in a gift (in a pair of boxes) should be divisible by k k k. In other words, two boxes i i i and j j j ( i i i j j j) can be combined as a gift if d i d_i di+ d j d_j dj is divisible by k k k.

How many boxes will Polycarp be able to give? Of course, each box can be a part of no more than one gift. Polycarp cannot use boxes “partially” or redistribute candies between them.

Input
The first line of the input contains two integers n n n and k k k ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1\leq n\leq2⋅10^5 1n2105, 1 ≤ k ≤ 100 1 \leq k \leq 100 1k100) — the number the boxes and the number the girls.

The second line of the input contains n integers d 1 , d 2 , … , d n d_1,d_2,…,d_n d1,d2,,dn ( 1 ≤ d i ≤ 1 0 9 1\leq d_i \leq 10^9 1di109), where d i d_i di is the number of candies in the i i i-th box.

Output
Print one integer — the maximum number of the boxes Polycarp can give as gifts.

Examples

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 ) (2,3) (2,3);
  • ( 5 , 6 ) (5,6) (5,6);
  • ( 1 , 4 ) (1,4) (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 ) (6,8) (6,8);
  • ( 2 , 3 ) (2,3) (2,3);
  • ( 1 , 4 ) (1,4) (1,4);
  • ( 5 , 7 ) (5,7) (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 ) (1,2) (1,2);
  • ( 6 , 7 ) (6,7) (6,7).

So the answer is 4.

题目大意

n n n个数,任意两个之间进行两两组合,其中必须满足两数之和能被 k k k整除,问最多有多少组?输出数字的数量即组数 ∗ 2 *2 2

解题思路

整除换而言之的意思就是余数为0。那么两数之和能被k整除的话就是两个数对k取余的余数之和为k。

AC代码
#include<bits/stdc++.h>
const int Max_N=2e5+6;
using namespace std;
int n,k,d[Max_N];
int main()
{
	map<int,int>mp;
	cin>>n>>k;
	int flag[101];//进行标记
	memset(flag,false,sizeof(flag));
	for(int i=1;i<=n;i++)
	{
		cin>>d[i];
	}
	for(int i=1;i<=n;i++)
	{
		int mid=d[i]%k;
		if(!mp.count(mid))
			mp[mid]=0;
		mp[mid]++;//对余数进行统计
	}
	int ans=mp[0]/2*2;//余数为0的话任意两个数都是满足条件的,但要注意这里的运算。
	for(int i=1;i<k;i++)
	{
		if(!flag[i])
		{
			if(i!=k-i)
				ans=ans+min(mp[i],mp[k-i])*2;
			else 
				ans=ans+min(mp[i],mp[k-i])/2*2;
			flag[i]=flag[k-i]=true;
		}
	}
	cout<<ans<<endl;
}
总结
找到问题的本质
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值