NC212616. DessertTime
描述
输入描述
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, , ,……, (1 ≤ ≤ ), 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"); } }