A. Special Permutation
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given one integer n (n>1).
Recall that a permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation of length 5, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).
Your task is to find a permutation p of length n that there is no index i (1≤i≤n) such that pi=i (so, for all i from 1 to n the condition pi≠i should be satisfied).
You have to answer t independent test cases.
If there are several answers, you can print any. It can be proven that the answer exists for each n>1.
Input
The first line of the input contains one integer t (1≤t≤100) — the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2≤n≤100) — the length of the permutation you have to find.
Output
For each test case, print n distinct integers p1,p2,…,pn — a permutation that there is no index i (1≤i≤n) such that pi=i (so, for all i from 1 to n the condition pi≠i should be satisfied).
If there are several answers, you can print any. It can be proven that the answer exists for each n>1.
Example
inputCopy
2
2
5
outputCopy
2 1
2 1 5 3 4
当1<=i<=n-1时输出i+1,当i ==n时输出1即可按照题意输出1-n中所有的数字。
#include <bits/stdc++.h>
using namespace std;
int n, t;
int main(){
scanf("%d", &t);
while(t--){
scanf("%d", &n);
if(n == 1) printf("1");
else for(int i = 1; i <= n; i++){
if(i != n) printf("%d ", i + 1);
else printf("1");
}
printf("\n");
}
return 0;
}
B. Unique Bid Auction
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
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
找到只出现一次并且最小的数字,输出下标即可。
#include <bits/stdc++.h>
using namespace std;
int n, t;
int a[200010];
int m[200010];
int main(){
scanf("%d", &t);
while(t--){
int minn = 1e9, temp;
memset(m, 0, sizeof(m));
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
m[a[i]]++;
}
for(int i = 1; i <= n; i++){
if(m[a[i]] == 1 && a[i] < minn){
minn = a[i];
temp = i;
}
}
if(minn != 1e9) printf("%d\n", temp);
else printf("-1\n");
}
return 0;
}