CodeForces - 1616A Integer Diversity【map】

A. Integer Diversity
time limit per test1 second
memory limit per test256 megabytes

You are given n integers a1,a2,…,an. You choose any subset of the given numbers (possibly, none or all numbers) and negate these numbers (i. e. change x→(−x)). What is the maximum number of different values in the array you can achieve?

Input
The first line of input contains one integer t (1≤t≤100): the number of test cases.

The next lines contain the description of the t test cases, two lines per a test case.

In the first line you are given one integer n (1≤n≤100): the number of integers in the array.

The second line contains n integers a1,a2,…,an (−100≤ai≤100).

Output
For each test case, print one integer: the maximum number of different elements in the array that you can achieve negating numbers in the array.

Example
input
3
4
1 1 2 2
3
1 2 3
2
0 0
output
4
3
1

Note
In the first example we can, for example, negate the first and the last numbers, achieving the array [−1,1,2,−2] with four different values.

In the second example all three numbers are already different.

In the third example negation does not change anything.

问题链接CodeForces - 1616A Integer Diversity
问题简述:(略)
问题分析:给出2个题解,各有技巧不同。0算1次,0以外的绝对值最多算2次。

AC的C++语言程序如下:

/* CodeForces - 1616A Integer Diversity */

#include <iostream>
#include <map>
#include <cstdio>

using namespace std;

int main()
{
    int t, n, a;
    scanf("%d", &t);
    while (t--) {
        map<int, int> m;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a);
            m[abs(a)]++;
        }

        int ans = 0;
        for (auto [x, y] : m)
            if (x == 0) ans++;
            else ans += min(2, y);

        printf("%d\n", ans);
    }

    return 0;
}

AC的C++语言程序如下:

/* CodeForces - 1616A Integer Diversity */

#include <iostream>
#include <map>
#include <cstdio>

using namespace std;

int main()
{
    int t, n, a;
    scanf("%d", &t);
    while (t--) {
        map<int, int> m;
        int cnt = 0;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a);
            if (m[a]) {
                if (m[-a])
                    ;
                else
                    m[-a] = 1, cnt++;
            } else
                m[a] = 1, cnt++;
        }

        printf("%d\n", cnt);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值