CodeForces - 1506D和CodeForces - 1497B

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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 749C Voting 是一道有趣的计数题,它的题意是给定了两个候选人 A 和 B 的得票情况,票数相等的情况下,我们需要按照一定的规则进行投票,直到最后一个候选人胜出。具体来说,如果当前的票数相等,我们需要按照轮流投票的规则,每次投给 A 或者 B,直到最后一个候选人胜出。如果某一时刻 A 的票数比 B 多 2 票以上,那么 A 就直接胜出,同样的,如果 B 的票数比 A 多 2 票以上,那么 B 就直接胜出。 我们可以用两个变量 sa 和 sb 来表示 A 和 B 的票数,然后依次遍历每一个投票者的选择。如果当前 A 和 B 的票数相等,我们就按照轮流投票的规则,每次投给 A 或者 B,直到最后一个候选人胜出。如果 A 和 B 的票数相差 2 票以上,那么直接输出胜出者的名字。最后一定会有一个候选人胜出,因为在每一轮投票之后,A 和 B 的票数都会有所增加,最终一定会有一个候选人胜出。 下面是该问题的 Java 代码实现: ``` import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String s = sc.next(); int sa = 0, sb = 0, pa = 0, pb = 0; boolean[] sign = new boolean[n]; for (int i = 0; i < n; i++) { if (s.charAt(i) == 'D') sa++; else sb++; } while (sa > 0 && sb > 0) { for (int i = 0; i < n; i++) { if (sign[i]) continue; if (s.charAt(i) == 'D') { if (pb > 0) { pb--; sa--; sign[i] = true; } else pa++; } else { if (pa > 0) { pa--; sb--; sign[i] = true; } else pb++; } if (sa == 0 || sb == 0) break; } } if (sa == 0) System.out.println("R"); else System.out.println("D"); } } ``` 该代码的时间复杂度为 $O(n)$,空间复杂度为 $O(n)$,其中 n 表示投票者的数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值