每个测试文件均包含多组测试数据。第一行输入一个整数
代表数据组数,每组测试数据描述如下:
第一行输入一个整数
代表卡牌种类数。
第二行输入
个整数
代表每种卡牌数量。
除此之外,保证每一组测试数据的卡牌总数之和不小于
;单个测试文件的
之和不超过
。
对于每一组测试数据,如果没有必胜策略,直接输出
;否则,在单独的一行上输出一个整数,代表你至少需要预知多少张卡牌,才能保证你不会输。
3 2 1 1 1 10 2 2 3
0 -1 3
对于第一组测试数据,只有两张卡牌,且各不相同,直接翻开即可。
对于第二组测试数据,由于卡牌种类唯一,不管怎么翻都会输。
T = int(input()) for i in range(T): n = int(input()) p = list(map(int,input().split())) if n==1: print(-1) else: if sum(p)==max(p)+n-1:#“除最大堆外,所有其他卡牌数量均为 1” print(max(p)-1)#随机预知k张;①预知的选一张,剩余的选一张。②预知的里面选两种 else: print(max(p))#①预知的选一张,剩余的选一张。②预知的里面选两种,k张牌中必然包含两种(对应②),或者只为数量最多的牌(对应①)。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void solve() {
int n;
cin >> n;
long long max_a = 0;
int count_ge_2 = 0; // 记录数量大于等于 2 的卡牌种类数
for (int i = 0; i < n; i++) {
long long a;
cin >> a;
if (a >= 2) {
count_ge_2++;
}
max_a = max(max_a, a);
}
// 只有一种卡牌,必死无疑
if (n == 1) {
cout << -1 << "\n";
return;
}
// 根据上面的推导进行判断
if (count_ge_2 == 0) {
// 没有任何重复的牌,闭着眼翻都能赢
cout << 0 << "\n";
} else if (count_ge_2 == 1) {
// 只有一种牌有重复,留一张在暗牌里当内鬼,暗牌堆就全是不重复的牌了
cout << max_a - 1 << "\n";
} else {
// 有多种牌存在重复,必须把最多的那种牌全看光
cout << max_a << "\n";
}
}
int main() {
// 算法竞赛起手式:解除输入输出的绑定,提升速度
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
} #include <iostream>
#include <vector>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int n;
int Max = 0, count = 0;
cin >> n;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] > Max) {
Max = a[i];
}
if (a[i] > 1) {
count++;
}
}
if (n == 1) {
cout << -1 << endl;
} else if (count == 0) {
cout << 0 << endl;
} else if (count == 1) {
cout << Max - 1 << endl;
} else {
cout << Max << endl;
}
}
return 0;
} import java.util.Scanner;
import java.util.Arrays;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int T = in.nextInt();
in.nextLine();
for(int i = 0;i < T;i++){
int n = in.nextInt();
in.nextLine();
int[] arr = new int[n];
for(int j = 0;j < n;j++){
arr[j] = in.nextInt();
}
in.nextLine();
if(n == 1){
System.out.println("-1");
}else{
int tmp = 0;
for(int k = 0;k < arr.length;k++){
if(arr[k] == 1){
tmp++;
}
}
if(tmp == arr.length){
System.out.println("0");
}else{
Arrays.sort(arr);
int num = 0;
for(int l = 0;l < arr.length;l++){
if(arr[l] == 1){
num++;
}
}
if(num == arr.length - 1){
System.out.println(arr[arr.length - 1] - 1);
}else{
System.out.println(arr[arr.length - 1]);
}
}
}
}
}
in.close();
}
} #include <algorithm>
#include <iostream>
#include <mutex>
#include <set>
#include <vector>
using namespace std;
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> vec;
int m;
int n_cpy = n;
while (n--) {
cin >> m;
vec.push_back(m);
}
if ( n_cpy == 1 ) {
cout << -1 << endl;
} else {
int one_number = 0;
for (int i : vec) {
if ( 1 == i ) {
one_number++;
}
}
if ( one_number == vec.size() ) {
cout << 0 << endl;
} else if( one_number == vec.size() - 1 ) {
cout << *max_element(vec.begin(), vec.end()) - 1 << endl;
}else {
cout << *max_element(vec.begin(), vec.end()) << endl;
}
}
}
}
// 64 位输出请用 printf("%lld")