#include <algorithm>
#include <bitset>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <random>
//#define int long long
#define loop(X, N) for(int X = 0; X < (N); X++)
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef uint64_t u64;
typedef int64_t i64;
vi p;
int solve(int l, int r, vi curP, int upperBound)
{
if (l == r)
return p[l] != curP[l];
int lowerBound = 0;
int lowestI = l, highestI = l;
for (int i = l; i <= r; i++)
{
if (curP[i] < curP[lowestI])
lowestI = i;
if (curP[i] > curP[highestI])
highestI = i;
lowerBound += curP[i] != p[i];
}
if (lowerBound >= upperBound)
return upperBound;
int result = upperBound;
swap(curP[l], curP[lowestI]);
int v1 = solve(l + 1, r, curP, result);
v1 += bool(curP[l] != p[l]);
swap(curP[l], curP[lowestI]);
result = min(result, v1);
swap(curP[r], curP[lowestI]);
int v2 = solve(l, r - 1, curP, result);
v2 += bool(curP[r] != p[r]);
swap(curP[r], curP[lowestI]);
result = min(result, v2);
swap(curP[l], curP[highestI]);
int v3 = solve(l + 1, r, curP, result);
v3 += bool(curP[l] != p[l]);
swap(curP[l], curP[highestI]);
result = min(result, v3);
swap(curP[r], curP[highestI]);
int v4 = solve(l, r - 1, curP, result);
v4 += bool(curP[r] != p[r]);
swap(curP[r], curP[highestI]);
result = min(result, v4);
return result;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
p.resize(n);
loop(i, n)
{
cin >> p[i];
p[i]--;
//p[i] = i;
}
//auto rd = random_device{};
//auto rng = default_random_engine{ rd() };
//std::shuffle(all(p), rng);
vi curP = p;
cout << solve(0, n - 1, curP, n) << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
6 ms |
448 KB |
Output is correct |
15 |
Correct |
10 ms |
452 KB |
Output is correct |
16 |
Correct |
14 ms |
348 KB |
Output is correct |
17 |
Correct |
82 ms |
428 KB |
Output is correct |
18 |
Correct |
66 ms |
344 KB |
Output is correct |
19 |
Correct |
64 ms |
348 KB |
Output is correct |
20 |
Correct |
57 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
6 ms |
448 KB |
Output is correct |
15 |
Correct |
10 ms |
452 KB |
Output is correct |
16 |
Correct |
14 ms |
348 KB |
Output is correct |
17 |
Correct |
82 ms |
428 KB |
Output is correct |
18 |
Correct |
66 ms |
344 KB |
Output is correct |
19 |
Correct |
64 ms |
348 KB |
Output is correct |
20 |
Correct |
57 ms |
348 KB |
Output is correct |
21 |
Execution timed out |
7063 ms |
348 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
6 ms |
448 KB |
Output is correct |
15 |
Correct |
10 ms |
452 KB |
Output is correct |
16 |
Correct |
14 ms |
348 KB |
Output is correct |
17 |
Correct |
82 ms |
428 KB |
Output is correct |
18 |
Correct |
66 ms |
344 KB |
Output is correct |
19 |
Correct |
64 ms |
348 KB |
Output is correct |
20 |
Correct |
57 ms |
348 KB |
Output is correct |
21 |
Execution timed out |
7063 ms |
348 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |