B. Unique Bid Auction
题面:
Example
inputCopy
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
outputCopy
-1
2
4
1
2
-1
题意分析:
输入n与n个数字,输出n个数字中只出现一次的数字,如有多个选取最小的那个输出,如没有则输出0。
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
while (T--) {
bool flag = 0;
int a[N], b[N] = {0};
ll n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b, b + n);
int l;
for (l = 0; l < n; l++) {
if (flag) break;
else {
if (l < n - 1 && b[l - 1] != b[l] && b[l] != b[l + 1]) {
for (int j = 0; j < n; j++) {
if (a[j] == b[l]) {
cout << j + 1 << endl;
flag = 1;
break;
}
}
} else if (l == n - 1 && b[l - 1] != b[l]) {
for (int j = 0; j < n; j++)
if (a[j] == b[l]) {
cout << j + 1 << endl;
flag = 1;
break;
}
}
}
}
if (!flag) cout << -1 << endl;
}
}