All Distinct

  Time Limit:1000ms Memory Limit:262144K

Description:

Sho has an array aa consisting of n integers. An operation consists of choosing two distinct indices i and j and removing ai and aj from the array.

For example, for the array [2,3,4,2,5], Sho can choose to remove indices 1 and 3. After this operation, the array becomes [3,2,5]. Note that after any operation, the length of the array is reduced by two.

After he made some operations, Sho has an array that has only distinct elements. In addition, he made operations such that the resulting array is the longest possible.

More formally, the array after Sho has made his operations respects these criteria:

  • No pairs such that (i<j) and ai=aj exist.
  • The length of a is maximized.

Output the length of the final array.

Input:

The first line contains a single integer tt (1≤t≤1000) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤50) — the length of the array.

The second line of each test case contains nn integers aiai (1≤ai≤10000) — the elements of the array.

Output:

For each test case, output a single integer — the length of the final array. Remember that in the final array, all elements are different, and its length is maximum.

Sample Input:

4
6
2 2 2 3 3 3
5
9 1 9 9 1
4
15 16 16 15
4
10 100 1000 10000

Sample Output:

2
1
2
4

Note:

For the first test case Sho can perform operations as follows:

  1. Choose indices 1 and 5 to remove. The array becomes [2,2,2,3,3,3]→[2,2,3,3].
  2. Choose indices 1 and 4 to remove. The array becomes [2,2,3,3]→[2,3].

The final array has a length of 2, so the answer is 2. It can be proven that Sho cannot obtain an array with a longer length.

For the second test case Sho can perform operations as follows:

  1. Choose indices 3 and 4 to remove. The array becomes [9,1,9,9,1]→[9,1,1].
  2. Choose indices 1 and 3 to remove. The array becomes [9,1,1]→[1].

The final array has a length of 1, so the answer is 1. It can be proven that Sho cannot obtain an array with a longer length.

分析

题目要求删除重复的,那么只需要计算出重复的数字就行(保留一个),比如某个数字出现了n次,记入的时候只需要记录n-1次即可,计算出所有的重复数字之后,删除的次数也得出了,一次删除两个数字,剩余数字的数量也可得出。

代码

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t>0){
        int n;
        int number=1;
        cin>>n;
        int data[n];
        for(int i=0;i<n;i++)
            cin>>data[i]; 
        sort(data,data+n);
        
        for(int i=1;i<n;i++)
            if(data[i]==data[i-1])
                number++;
        cout<<n-number/2*2<<endl;
        t--;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值