D 预知

本题使用到了鸽巢原理(抽屉原理)的思想。

首先考虑 的情况,由于总卡牌数 ,故这是必败的。

时,看样例也能发现分为几种情况:

  • 全是 时,说明随便翻两张都必胜,所以
  • 只有一个 时,例如 这种情况,很多同学以为要预知 张,其实只要 张就够了,因为预知出来的两张都一样的话,说明剩下的四张就成了全 的情况,所以
  • 其他情况, ,因为我们预知 张相同的卡牌后,就知道了剩下的所有卡牌和已经知道的卡牌都是不一样的,从这两堆里各选一张即可。
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;

int a[N];

void solve() {
    int n; cin >> n;
    for (int i = 1; i <= n; i ++ ) cin >> a[i];
    if (n == 1) {
        cout << -1 << endl;
        return ;
    }
    int mx = -1;
    for (int i = 1; i <= n; i ++){
        mx = max(mx, a[i]);
    }
    if (mx == 1) cout << 0 << endl;
    else {
        int cnt = 0;
        for (int i = 1; i <= n; i ++ ) {
            if (a[i] > 1) cnt ++;
        }
        if (cnt == 1) cout << mx - 1 << endl;
        else cout << mx << endl;
    }
}

int main(){
    int T; cin >> T;
    while (T -- ) solve();
    return 0;
}