Card Eater(AtCoder-2299)

Problem Description

Snuke has decided to play a game using cards. He has a deck consisting of N cards. On the i-th card from the top, an integer Ai 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, N 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≦105
  • N is odd.
  • 1≦Ai≦105
  • Ai is an integer.

Input

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

N
A1 A2 A3 ... AN

Output

Print the answer.

Example

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 1 and one card with 2. One card with 1 and another with 2 will be eaten, and the remaining card with 1 will be returned to deck. Then, the values written on the remaining cards in the deck will be pairwise distinct: 1, 3 and 7.

Sample Input 2

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

Sample Output 2

7

题意:有 n 张牌,现在可对这 n 张牌进行任意次操作,使得操作结束后牌没有相同的,每次操作为从这些牌中任选 3 张,去除掉这 3 张中最大与最小的牌,问操作结束后牌的数量

思路:

可以发现,只要有两张相同的,那么就可以选这两个牌再任选一张,然后把这两张牌的任一张与任选的一张扔掉

因此,当种类数为奇数时,无论是否有重复,保存下来的总为种类数的个数;当种类数为偶数时,无论是否有重复,一定会多选出一张牌来去掉

故对于这 n 张牌,先去枚举这些牌的种类数,然后去模 2 看种类数为奇数还是偶数,若为奇数,则直接输出种类数,若为偶数,则输出种类数-1

Source Program

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
int bucket[N];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        bucket[x]++;
    }

    int type=0;
    for(int i=1;i<=N;i++)
        if(bucket[i]!=0)
            type++;

    int res;
    if(type%2)
        res=type;
    else
        res=type-1;

    printf("%d\n",res);
    return 0;
}

 

你可能感兴趣的:(#,AtCoder,#,常用技巧——桶排)