제출 #1000765

#제출 시각아이디문제언어결과실행 시간메모리
1000765j_vdd16Giraffes (JOI22_giraffes)C++17
32 / 100
7056 ms592 KiB
#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>

//#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)
{
	if (l == r)
		return p[l] != curP[l];

	int lowestI = l, highestI = l;
	for (int i = l + 1; i <= r; i++)
	{
		if (curP[i] < curP[lowestI])
			lowestI = i;
		if (curP[i] > curP[highestI])
			highestI = i;
	}

	swap(curP[l], curP[lowestI]);
	int v1 = solve(l + 1, r, curP);
	v1 += bool(curP[l] != p[l]);
	swap(curP[l], curP[lowestI]);

	swap(curP[r], curP[lowestI]);
	int v2 = solve(l, r - 1, curP);
	v2 += bool(curP[r] != p[r]);
	swap(curP[r], curP[lowestI]);

	swap(curP[l], curP[highestI]);
	int v3 = solve(l + 1, r, curP);
	v3 += bool(curP[l] != p[l]);
	swap(curP[l], curP[highestI]);

	swap(curP[r], curP[highestI]);
	int v4 = solve(l, r - 1, curP);
	v4 += bool(curP[r] != p[r]);
	swap(curP[r], curP[highestI]);

	return min({ v1, v2, v3, v4 });
}

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]--;
	}

	vi curP = p;
	cout << solve(0, n - 1, curP) << endl;

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...