CodeForces 864A Fair Game

題目鏈接:http://codeforces.com/contest/864/problem/A
題意:有一個遊戲,給你一個長度爲n的序列,兩個人分別選兩個數字,你選擇了一個數字,你就能把這串序列的所有數字拿走,現在讓你判斷這個遊戲是否公平,如果選完這兩個數字,就把所有的數字拿走了,且兩個人手上的數字個數是一樣的,就稱爲公平遊戲,否則不公平
解析:其實說白了,就問你這個序列是否有兩種數字,且個數相等,我是直接拿map存着,然後判斷一下

#include <bits/stdc++.h>
using namespace std;
map<int,int>maple;
int main(void)
{
    int n,x;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&x);
        maple[x]++;
    }
    if(maple.size()>2)
        puts("NO");
    else
    {
        map<int,int>::iterator t1 = maple.begin();
        map<int,int>::iterator t2 = maple.begin();t2++;
        if(t1->second==t2->second)
        {
            puts("YES");
            printf("%d %d\n",t1->first,t2->first);
        }
        else
            puts("NO");
    }

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章