暴力解题的暴力题解……我以为会超时,结果它放过了我……
下面是题目描述:
There is a game called "Unique Bid Auction". You can read more about it here: https://en.wikipedia.org/wiki/Unique_bid_auction (though you don't have to do it to solve this problem).
Let's simplify this game a bit. Formally, there are n participants, the i-th participant chose the number ai. The winner of the game is such a participant that the number he chose is unique (i. e. nobody else chose this number except him) and is minimal (i. e. among all unique values of a the minimum one is the winning one).
Your task is to find the index of the participant who won the game (or -1 if there is no winner). Indexing is 1-based, i. e. the participants are numbered from 1 to n.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1≤n≤2⋅105) — the number of participants. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤n), where ai is the i-th participant chosen number.
It is guaranteed that the sum of n does not exceed 2⋅105 (∑n≤2⋅105).
Output
For each test case, print the answer — the index of the participant who won the game (or -1 if there is no winner). Note that the answer is always unique.
Example
input
6
2
1 1
3
2 1 3
4
2 2 2 3
1
1
5
2 3 2 4 2
6
1 1 5 5 4 4
output
-1
2
4
1
2
-1
下面是解题过程:
- 从本题来看,需要选取有且仅有一的最小值。所以,我们首先需要判断最小值,然后判断是否唯一。
- 在输入过程中,我们需要统计该数字一共出现了几次。
- 将数组的序号用另外一个数组存入,然后将该数组排序,获得最小值。
- 判断该最小值是否唯一。
- 如果上述条件不满足,那么输出-1;如果满足,则找到在原数组对应的编号进行输出。
下面是AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
const int N=1e5*3;
using namespace std;
int n;
int a[N],b[N];
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
int c[N]= {0};
scanf("%d",&n);
for(int i=1; i<n+1; i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
c[a[i]]++;
}
sort(a+1,a+n+1);
int num=-1,flag=0;
for(int i=1; i<n+1; i++)
{
if(c[a[i]]==1)
{
num=a[i];
flag=1;
break;
}
}
if(flag==0) printf("-1\n");
else
{
for(int i=1; i<n+1; i++)
{
if(b[i]==num)
{
printf("%d\n",i);
break;
}
}
}
}
return 0;
}