• AtCoder Beginner Contest 053 D


    Problem Statement

     

    Snuke has decided to play a game using cards. He has a deck consisting of NN cards. On the ii -th card from the top, an integer AiAi is written.

    He will perform the operation described below zero or more times, so that the values written on the remaining cards will be pairwise distinct. Find the maximum possible number of remaining cards. Here, NN is odd, which guarantees that at least one card can be kept.

    Operation: Take out three arbitrary cards from the deck. Among those three cards, eat two: one with the largest value, and another with the smallest value. Then, return the remaining one card to the deck.

    Constraints

     

    • 3N1053≦N≦105
    • NN is odd.
    • 1Ai1051≦Ai≦105
    • AiAi is an integer.

    Input

     

    The input is given from Standard Input in the following format:

    NN
    
    
    A1A1
    
     A2A2
    
     A3A3
    
     ... ANAN
    
    
    

    Output

     

    Print the answer.

    Sample Input 1

     

    5
    1 2 1 3 7
    

    Sample Output 1

     

    3
    

    One optimal solution is to perform the operation once, taking out two cards with 11 and one card with 22 . One card with 11 and another with 22 will be eaten, and the remaining card with 11 will be returned to deck. Then, the values written on the remaining cards in the deck will be pairwise distinct: 11 , 33 and 77 .

    Sample Input 2

     

    15
    1 3 5 2 1 3 2 8 8 6 2 6 11 1 1
    

    Sample Output 2

     

    7
    

    Sponsor

    挺不错的签到题。每次抽三张放回一张相当于抽两张不放回,既然要求最终剩余的卡片最多,可以贪心地考虑,每次优先抽取多出来的重复的卡片,如果这些多出来的重复卡片是偶数张,那么把它们全抽完;如果是奇数张则多抽一张来凑数,这样能满足剩下的最多。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    int n,a[100005]={0};
    int main()
    {
        cin>>n;
        int i;
        for(i=1;i<=n;i++)
        {
            int temp;
            scanf("%d",&temp);
            a[temp]++;
        }
        int sum=0,res=0;
        for(i=1;i<=100005;i++)
        {
            sum+=a[i];
            if(a[i]>1)res+=a[i]-1;
        }
        if(res%2==0)
        {
            cout<<sum-res;
        }
        else
        {
            cout<<sum-res-1;
        }
        return 0;
     } 
  • 相关阅读:
    AngularJS中的Provider们:Service和Factory等的区别
    解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法
    关于EL表达式不起作用的问题
    Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群,Tomcat集群
    Java WebService 简单实例
    火狐浏览器中表单内容在表单刷新时候不重置表单信息
    ie文本框内容不居中问题
    javascript call和apply方法
    javascript的词法作用域
    C++提高编程 deque容器
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12590439.html
Copyright © 2020-2023  润新知