AtCoder Beginner Contest 053 D - Card Eater(思维)

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

  • 3≦N≦1053≦N≦105
  • NN is odd.
  • 1≦Ai≦1051≦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;
 } 

原文地址:https://www.cnblogs.com/lipoicyclic/p/12590439.html

时间: 2024-08-30 12:34:50

AtCoder Beginner Contest 053 D - Card Eater(思维)的相关文章

AtCoder Beginner Contest 124 D - Handstand(思维+前缀和)

D - Handstand Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement NN people are arranged in a row from left to right. You are given a string SS of length NN consisting of 0 and 1, and a positive integer KK. The ii-th per

AtCoder Beginner Contest 103 D(贪心)

AtCoder Beginner Contest 103 D 题目大意:n个点,除第n个点外第i与第i+1个点有一条边,给定m个a[i],b[i],求最少去掉几条边能使所有a[i],b[i]不相连. 按右端点从小到大排序,如果当前选的去掉的边在区间内,那么符合条件,否则ans++,并贪心地把去掉的边指向右端点,因为前面的区间都满足条件了,所以要去掉的边要尽量向右移使其满足更多的区间. 1 #include <iostream> 2 #include <cstdio> 3 #incl

AtCoder Beginner Contest 136

AtCoder Beginner Contest 136 Contest Duration : 2019-08-04(Sun) 20:00 ~ 2019-08-04(Sun) 21:40 Website: AtCoder BC-136 后面几题都挺考思考角度D. C - Build Stairs 题目描述: 有n座山从左到右排列,给定每一座山的高度\(Hi\),现在你可以对每座山进行如下操作至多一次:将这座山的高度降低1. 问是否有可能通过对一些山进行如上操作,使得最后从左至右,山的高度呈不下降

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one

AtCoder Beginner Contest 155 简要题解

AtCoder Beginner Contest 155 A:签到失败,WA一次. int main() { int a, b, c; cin >> a >> b >> c; if(a == b && b == c) cout << "No"; else if(a == b || a == c || b == c) cout << "Yes"; else cout << &quo

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质)

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质) We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and Vertex bibi. Consider painting each of these edges white or black. There ar

【ATcoder】AtCoder Beginner Contest 161 题解

题目链接:AtCoder Beginner Contest 161 原版题解链接:传送门 A - ABC Swap 这题太水,直接模拟即可. 1 #include <iostream> 2 using namespace std; 3 int main() { 4 int a, b, c; 5 cin >> a >> b >> c; 6 swap(a, b); 7 swap(a, c); 8 cout << a << " &

AtCoder Beginner Contest 100 C(思维)

*3 or /2题目大意:有n个数,每次操作将第i个数*3或/2,得到结果必须为整数,且每次操作必须有一次为/2,求最大操作次数.一开始看很懵比,感觉肯定是思维题,对着样例猜了个结论竟然就过了大数据...思路:奇数只能 * 3,所以只考虑偶数.对于一个偶数,可以分解成2^n * a,显然a为奇数,那么如果这个偶数乘3为2^n * 3a,3a也显然为奇数,所以证得:任意一个偶数无论乘多少次3它能/2的次数仍然为n不变.由于次数最大,所以每次只让一个偶数/2.所以答案就是统计每个偶数为2^n * a

AtCoder Beginner Contest 116 C题 【题意:可以在任意区间【L,R】上加1,求通过最少加1次数得到题目给定的区间】】{思维好题}

C - Grand Garden In a flower bed, there are NN flowers, numbered 1,2,......,N1,2,......,N. Initially, the heights of all flowers are 00. You are given a sequence h={h1,h2,h3,......}h={h1,h2,h3,......} as input. You would like to change the height of