栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

CodeForces - 1616A Integer Diversity【map】

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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++语言程序如下:


#include 
#include 
#include 

using namespace std;

int main()
{
    int t, n, a;
    scanf("%d", &t);
    while (t--) {
        map 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("%dn", ans);
    }

    return 0;
}

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


#include 
#include 
#include 

using namespace std;

int main()
{
    int t, n, a;
    scanf("%d", &t);
    while (t--) {
        map 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("%dn", cnt);
    }

    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/876268.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号