• D. Epic Transformation


    Problem - 1506D - CodeforcesD. Epic Transformation

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You are given an array aa of length nn consisting of integers. You can apply the following operation, consisting of several steps, on the array aa zero or more times:

    • you select two different numbers in the array aiai and ajaj;
    • you remove ii-th and jj-th elements from the array.

    For example, if n=6n=6 and a=[1,6,1,1,4,4]a=[1,6,1,1,4,4], then you can perform the following sequence of operations:

    • select i=1,j=5i=1,j=5. The array aa becomes equal to [6,1,1,4][6,1,1,4];
    • select i=1,j=2i=1,j=2. The array aa becomes equal to [1,4][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 tt (1≤t≤1041≤t≤104). Then tt test cases follow.

    The first line of each test case contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) is length of the array aa.

    The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

    It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

    Output

    For each test case, output the minimum possible size of the array after applying some sequence of operations to it.

    Example

    input

    Copy

    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

    Copy

    0
    0
    2
    1
    0
    =======================================================================================

    很老的贪心,只需要每次取出来最大的两个数字就行了,使用优先队列和map即可

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. using namespace std;
    12. typedef long long int ll;
    13. priority_queue<int>q;
    14. map<int,int>mp;
    15. int main()
    16. {
    17. int t;
    18. cin>>t;
    19. while(t--)
    20. {
    21. int n;
    22. cin>>n;
    23. mp.clear();
    24. while(!q.empty())
    25. q.pop();
    26. for(int i=1;i<=n;i++)
    27. {
    28. int x;
    29. cin>>x;
    30. mp[x]++;
    31. }
    32. for(auto it:mp)
    33. {
    34. q.push(it.second);
    35. }
    36. while(q.size()>=2)
    37. {
    38. int now=q.top();
    39. q.pop();
    40. int now1=q.top();
    41. q.pop();
    42. now--;
    43. now1--;
    44. if(now)
    45. q.push(now);
    46. if(now1)
    47. q.push(now1);
    48. }
    49. if(q.size()==0)
    50. {
    51. cout<<0<
    52. }
    53. else
    54. {
    55. cout<top()<
    56. }
    57. }
    58. return 0;
    59. }

  • 相关阅读:
    阿里老哥独家珍藏的Java面试突击宝典,轻松应对95%秋招面试题
    SpringBoot配置文件(properties、yml、yaml)
    计算机网络之应用层面试题
    浅谈“天线和通信历史“
    数字逻辑·逻辑代数【常用公式、化简】
    2024.1IDEA 到2026年
    k8s--基础--12.5--pod--名称空间,标签,节点名称
    POJ 3977 Subset 折半枚举+二分搜素+双指针
    优咔科技创新连接方案助力高质量5G车联服务
    关于我们编写好的java程序是如何运行部署的
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126184116