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; }