B. All Distinct

(大家好,拖更了这么久,本博主又回来了)。

B. All Distinct

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Sho has an array aa consisting of nn integers. An operation consists of choosing two distinct indices ii and jj and removing aiai and ajaj 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 aa is maximized.

Output the length of the final array.

Input

The first line contains a single integer tt (1≤t≤103) — 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≤104) — 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.

Example

input

Copy

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

output

Copy

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 22, so the answer is 22. 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.

首先我们要知道,我们需要找到一个最大长度且数组元素不重复的数组。

接着我们把数组分区,将每个数组元素的个数重建一个数组。

如果该数组元素的数量是奇数个,我们就置位1

如果该数组元素的数量是偶数个,我们就置位2

那么我就开始讨论数组中元素的等可能的情况。

(注讨论的情况,重复是指该数组值出现等于两次,数组数量为1的情况,我们直接加就完事了)

长度默认为0

1.数组中元素全不重复

2数组中元素有偶数个重复

例如 1 1 2 2

等于 1 2

长度加有多少个重复

3.数组中元素有奇数个重复

例如 1 1 2 2 3 3

等于 1 2

长度加有多少个重复-1

思路已经分析完了,接下来我直接贴一下代码。

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t > 0)
    {
        int n, index1 = 0, sum = 0;
        cin >> n;
        int data1[10000];
        int qu[20000];
        for (int i = 0; i < n; i++)
        {
            cin >> data1[i];
        }
        sort(data1, data1 + n);
        for (int i = 0; i <= data1[n - 1]; i++)
        {
            qu[i] = 0;
        }
        for (int i = 0; i < n; i++)
        {
            qu[data1[i]]++;
        }
        for (int i = 0; i <= data1[n - 1]; i++)
        {
            if (qu[i] != 0)
            {
                if (qu[i] % 2 == 0)
                {
                    qu[i] = 2;
                }
                else
                {
                    qu[i] = 1;
                }
            }
        }
        for (int i = 1; i <= data1[n - 1]; i++)
        {
            if (qu[i] == 2)
            {
                index1++;
            }
        }
        if (index1 != 0)
        {
            if (index1 % 2 == 0)
            {
                sum = index1;
            }
            else
            {
                index1 = index1 - 1;
                sum = index1;
            }
            for (int i = 1; i <= data1[n - 1]; i++)
            {
                if (qu[i] == 1)
                {
                    sum++;
                }
            }
            cout << sum << endl;
        }
        else if (index1 == 0)
        {
            for (int i = 1; i <= data1[n - 1]; i++)
            {
                if (qu[i] == 1)
                {
                    sum++;
                }
            }
            cout << sum << endl;
        }
        t--;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
distinct的作用是去除查询结果中的重复记录,而union all则是将多个查询结果合并在一起,不去除重复记录。在使用union all后再使用distinct,相当于先将多个查询结果合并,然后再去除其中的重复记录。这样做的效率相对较低,因为需要对合并后的结果进行排序。而使用exists代替distinct可以避免排序,提高查询效率。所以在多表查询时,建议使用union all代替union,并使用exists代替distinct。[1][2] 举个例子来说明,假设有两个表TABLE_R和TABLE_BAL,它们的结构相同,都包含ACNO、DAT、LOGACNO、SENO和PROVICEID这几个字段。如果我们想要查询这两个表中的所有记录,并去除重复记录,可以使用以下语句: SELECT DISTINCT * FROM ( SELECT trim(ACNO) as ACNO, DAT, LOGACNO, SENO, PROVICEID FROM TABLE_R UNION ALL SELECT trim(ACNO) as ACNO, DAT, LOGACNO, SENO, PROVICEID FROM TABLE_BAL ) 这样的查询会先将两个表的记录合并,然后再去除重复记录。但是这个查询的效率相对较低,因为需要对合并后的结果进行排序。而如果我们使用exists代替distinct,可以提高查询效率: SELECT * FROM TABLE_R R WHERE EXISTS ( SELECT 1 FROM TABLE_BAL B WHERE trim(B.ACNO) = trim(R.ACNO) AND B.DAT = R.DAT AND B.LOGACNO = R.LOGACNO AND B.SENO = R.SENO AND B.PROVICEID = R.PROVICEID ) 这样的查询不需要进行排序,效率较高。所以在多表查询时,使用exists代替distinct可以提高查询效率。[3]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值