#include<iostream>
using namespace std;
int n;
int p[1005];
bool occupied[1005];
bool verif(int lady)
{
for(int i=1;i<=n;i++)
occupied[i]=0;
occupied[p[lady]]=1;
int assigned=0;
for(int i=1;i<=n;i++)
{
assigned++;
if(i==lady)
{
assigned--;
continue;
}
if(!occupied[assigned])
{
if(p[i]!=assigned)
return 0;
occupied[p[i]]=1;
}
else
{
if(p[i]==assigned)
return 0;
occupied[p[i]]=1;
}
}
return 1;
}
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
int t;
cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>p[i];
}
int unde=-1;
for(int i=1;i<=n;i++)
{
if(verif(i))
{
// cout<<i<<" ";
// continue;
if(unde==-1)
unde=p[i];
else
{
unde=0;
break;
}
}
}
// cout<<" zzz\n";
cout<<unde<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |