CodeForces - 1506D
You are given an array a of length n consisting of integers. You can apply the following operation, consisting of several steps, on the array a zero or more times:
you select two different numbers in the array ai and aj;
you remove i-th and j-th elements from the array.
For example, if n=6 and a=[1,6,1,1,4,4], then you can perform the following sequence of operations:
select i=1,j=5. The array a becomes equal to [6,1,1,4];
select i=1,j=2. The array a becomes equal to [1,4].
What can be the minimum size of the array after applying some sequence of operations to it?
Input
The first line contains a single integer t (1≤t≤104). Then t test cases follow.
The first line of each test case contains a single integer n (1≤n≤2⋅105) is length of the array a.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109).
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
Output
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
Example
Input
5
6
1 6 1 1 4 4
2
1 2
2
1 1
5
4 5 4 5 4
6
2 3 2 1 3 1
Output
0
0
2
1
0
每次将两个不同的数删去,最后剩下多少数。
优先队列,每次删去数量最大的两个数;
#include<bits/stdc++.h>
using namespace std;
int a[1000000];
int m[1000000];
int main()
{
int p;
scanf("%d",&p);
while(p--)
{
priority_queue<int, vector<int>, less<int> > q;
memset(m,0,sizeof(m));
int n,i,j,l=1;
scanf("%d",&n);
for(i=1; i<=n; i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
m[l]=1;
for(i=2;i<=n;i++)
{
if(a[i]==a[i-1])
{
m[l]++;
}
else
{
l++;
m[l]++;
}
}
for(i=1;i<=l;i++)
q.push(m[i]);
while(q.size()>1)
{
int x=q.top();
q.pop();
int y=q.top();
q.pop();
x--;
y--;
if(x)
q.push(x);
if(y)
q.push(y);
}
if(q.empty())
printf("0\n");
else
printf("%d\n",q.top());
}
}
CodeForces - 1497B
You are given an array a1,a2,…,an consisting of n positive integers and a positive integer m.
You should divide elements of this array into some arrays. You can order the elements in the new arrays as you want.
Let’s call an array m-divisible if for each two adjacent numbers in the array (two numbers on the positions i and i+1 are called adjacent for each i) their sum is divisible by m. An array of one element is m-divisible.
Find the smallest number of m-divisible arrays that a1,a2,…,an is possible to divide into.
Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.
The first line of each test case contains two integers n, m (1≤n≤105,1≤m≤105).
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109).
It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 105.
Output
For each test case print the answer to the problem.
Example
Input
4
6 4
2 2 8 6 9 4
10 8
1 1 1 5 2 4 4 8 6 7
1 1
666
2 2
2 4
Output
3
6
1
1
Note
In the first test case we can divide the elements as follows:
[4,8]. It is a 4-divisible array because 4+8 is divisible by 4.
[2,6,2]. It is a 4-divisible array because 2+6 and 6+2 are divisible by 4.
[9]. It is a 4-divisible array because it consists of one element.
将数组中的元素进行任意分割,使任意两个相邻元素的和为m的倍数。问最少分成几组
先将数组每个元素对m取余,将余数为0的放到一组中,将余数相加等于m的放进一组中
#include<bits/stdc++.h>
using namespace std;
int a[1001011];
int main()
{
int p;
scanf("%d",&p);
while(p--)
{
memset(a,0,sizeof(a));
int n,m,i,j,k;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d",&k);
a[k%m]++;
}
int sum=0;
if(a[0])
sum++;
for(i=1;i<=m/2;i++)
{
if(a[i]!=0&&a[i]==a[m-i])
sum++;
else
sum+=abs(a[i]-a[m-i]);
}
printf("%d\n",sum);
}
}