列表

详情


NC212616. DessertTime

描述

The party began, the greasy uncle was playing cards, the fat otaku was eating, and the little beauty was drawing.

Because there were too many steaks just grilled, ZJH found that he could not finish it, so he gave some of it to others. After LZT felt too greasy, he asked the kitchen to make some cakes for dessert. The kitchen put the finished cakes into many plates, each with several cakes.

At this time, LZT was talking with a friend. To show humility, people do not like to take the last plate and persuade each other to take more. So LZT and his friends always took a plate which has no fewer cakes than the other take last time. When a person found that there is no plate on the table that has no fewer cakes than the other take last time, he must take all the remaining cakes (if had), we usually called it "dōu dǐ" in Chinese.

Neither of them wanted to take the last plate, and Chairman Luo could not personally play due to his status, so the task was handed over to his assistant, that is, you.

Now given a number N, indicating the number of plates, and an array a, representing the number of cakes on each plate. During one move, the player chooses an existing number x and takes away a plate with cake number x. The next player's choice of x cannot be less than this time. Of course, the first choice is unlimited. The person who takes the last plate loses, and the person who cannot choose x will take all the plates left on the table. You are asked to tell Chairman Luo the first x to win, or -1 if he cannot win. Because of his status, Chairman Luo will always take the first move.

输入描述

There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 10), indicating the number of test cases. For each test case:

The first line contains an integer N (3 ≤ N ≤ ), indicating the number of plates.

The second line contains N integers, a_1 , a_2 ,……, a_n (1 ≤ a_i), indicating the number of cakes on i-th plate. It is guaranteed every number appears more than once.

输出描述

For each test case, each line contains an integer, indicating the first x Chairman Luo should choose to win or -1 if he can't win.

示例1

输入:

3
2
1 1
4
1 2 1 2
5
2 2 2 6 6

输出:

1
1
-1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 113ms, 内存消耗: 10304K, 提交时间: 2020-10-05 21:32:41

#include<cstdio>
#include<algorithm>
using namespace std;
int n,a[100005],ans[4],len[4],now,flag,pre;
int main() {
	int t;
	scanf("%d",&t);
	while(t--) {
		scanf("%d",&n);
		for(int i=1; i<=n; i++)scanf("%d",&a[i]);
		sort(a+1,a+n+1);
		int cnt=1;
		for(int i=n-1;i>=0;--i){
			if(a[i]==a[i+1])cnt++;
			else {
				if(cnt&1){
					if(i)printf("%d\n",a[i+1]);
					else printf("-1\n");
					goto  JUMP; 
				}
				cnt=1;
			}
		}
		printf("%d\n",a[1]);
		JUMP:;
	}
}

C++(clang++ 11.0.1) 解法, 执行用时: 161ms, 内存消耗: 816K, 提交时间: 2022-12-07 20:38:49

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
bool cmp(int a,int b){
	return a>b;
}
int a[N];
int main(){
	int t,n;
	cin>>t;
	while(t--){
		cin>>n;
		int ans=-1,f=0;
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		sort(a,a+n,cmp);
		int cnt=1;
		for(int i=1;i<n;i++){
			if(a[i]!=a[i-1]){
				if(cnt&1){
					ans=a[i-1];
					break;
				}
				cnt=1;
			}else cnt++;
		}
		if(ans==-1&&(!(cnt&1))) ans=a[n-1];
		cout<<ans<<endl;
	}
	return 0;
}

C++(g++ 7.5.0) 解法, 执行用时: 182ms, 内存消耗: 1344K, 提交时间: 2023-01-15 17:20:52

#include<bits/stdc++.h>
#define maxn 100007
int A[maxn];
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n,m,q;
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d",&A[i]);	
		}
		A[n]=0;
		std::sort(A,A+n);
		q=A[0];
		int x,i=n-1,f=1;
		while(i){
			while(i&&A[i--]==A[i]);
		    x=n-i-1;
		    if((x%2&&i)||(i==0&&n%2==0)){
			printf("%d\n",A[i+1]);
			f=0;break;
			}
		}
		if(f) printf("-1\n");
	}
}

上一题