#include<bits/stdc++.h>
using namespace std;
#define nl '\n'
inline void solve(){
int n;
cin>>n;
int a[n];
for(int i=0; i<n; i++) cin>>a[i];
vector<int> v(n);
iota(v.begin(), v.end(), 1);
int ans = 1e9;
do{
auto b = v;
bool tf = true;
for(int i=0; i<n-1; i++){
if(b[i] >= b[i+1]+2){
tf = false;
break;
}
}
if(!tf) continue;
int subans = 0;
for(int i=0; i<n; i++){
if(a[i] == b[i]) continue;
for(int j=i+1; j<n; j++){
if(a[i] != b[j]) continue;
for(int k=j; k>i; k--){
swap(b[k], b[k-1]);
subans++;
if(subans >= ans) break;
}
break;
}
if(subans >= ans) break;
}
ans = min(ans, subans);
}while(next_permutation(v.begin(), v.end()));
cout<<ans<<nl;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);cout.tie(NULL);
int t = 1;
//cin>>t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
10 ms |
336 KB |
Output is correct |
8 |
Correct |
7 ms |
336 KB |
Output is correct |
9 |
Correct |
7 ms |
336 KB |
Output is correct |
10 |
Correct |
7 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
10 ms |
336 KB |
Output is correct |
8 |
Correct |
7 ms |
336 KB |
Output is correct |
9 |
Correct |
7 ms |
336 KB |
Output is correct |
10 |
Correct |
7 ms |
336 KB |
Output is correct |
11 |
Execution timed out |
5047 ms |
336 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
10 ms |
336 KB |
Output is correct |
8 |
Correct |
7 ms |
336 KB |
Output is correct |
9 |
Correct |
7 ms |
336 KB |
Output is correct |
10 |
Correct |
7 ms |
336 KB |
Output is correct |
11 |
Execution timed out |
5047 ms |
336 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
10 ms |
336 KB |
Output is correct |
8 |
Correct |
7 ms |
336 KB |
Output is correct |
9 |
Correct |
7 ms |
336 KB |
Output is correct |
10 |
Correct |
7 ms |
336 KB |
Output is correct |
11 |
Execution timed out |
5047 ms |
336 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
10 ms |
336 KB |
Output is correct |
8 |
Correct |
7 ms |
336 KB |
Output is correct |
9 |
Correct |
7 ms |
336 KB |
Output is correct |
10 |
Correct |
7 ms |
336 KB |
Output is correct |
11 |
Execution timed out |
5047 ms |
336 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |