Codeforces Round #544 (Div. 3) Codeforces Round #544 (Div. 3)

International Women's Day is coming soon! Polycarp is preparing for the holiday.
There are
n
n
candy boxes in the shop for sale. The
i
i
-th box contains
d
i
di
candies.
Polycarp wants to prepare the maximum number of gifts for
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
. In other words, two boxes
i
i
and
j
j
(
i≠j
i≠j
) can be combined as a gift if
d
i

  • d
    j
    di+dj
    is divisible by
    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
    and
    k
    k
    (
    1≤n≤2⋅
    10
    5
    ,1≤k≤100
    1≤n≤2⋅105,1≤k≤100
    ) — the number the boxes and the number the girls.
    The second line of the input contains
    n
    n
    integers
    d
    1
    ,
    d
    2
    ,…,
    d
    n
    d1,d2,…,dn
    (
    1≤
    d
    i

    10
    9
    1≤di≤109
    ), where
    d
    i
    di
    is the number of candies in the
    i
    i
    -th box.
    Output
    Print one integer — the maximum number of the boxes Polycarp can give as gifts.
    Examples
    Input
    Copy
    7 2
    1 2 2 3 2 4 10
    Output
    Copy
    6
    Input
    Copy
    8 2
    1 2 2 3 2 4 6 10
    Output
    Copy
    8
    Input
    Copy
    7 3
    1 2 2 3 2 4 5
    Output
    Copy
    4

    题解:题意给n个数和k,从中取两个数,使得两个数之和能被k整除,每个数只能取一次.求最多能使用多少数.
    我的思路:先求出每个数除以k的余数.两个数的余数之和等于k就是符合条件的.例如,n为余数是1的数的个数和m是余数是k-1的数的个数,那么有min(n,m)2个数符合条件.a是余数是0的数的个数,符合条件的数为a/22;
#include <bits/stdc++.h>
const int N=2e5+5;
typedef long long ll;
using namespace std;
//int a[N];
int b[105];
int main(){
    int n,k;
    scanf("%d%d",&n,&k);
    int tmp;
    for(int i=1;i<=n;i++){
        scanf("%d",&tmp);
        b[tmp%k]++;
        //printf("%d=%d\n",i,tmp%k);
    }
    int ans=0;
    tmp=k/2;
    if(k%2==0) tmp=k/2-1,ans+=b[k/2]/2*2;
    for(int i=1;i<=tmp;i++){
        ans+=2*min(b[i],b[k-i]);
        //cout<<"ans="<<ans<<endl;
    }
    printf("%d\n",ans+b[0]/2*2);
    return 0;

}

猜你喜欢

转载自www.cnblogs.com/-yjun/p/10503402.html