Submission #831728

# Submission time Handle Problem Language Result Execution time Memory
831728 2023-08-20T12:49:11 Z green_gold_dog Group Photo (JOI21_ho_t3) C++17
100 / 100
666 ms 118868 KB
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>

using namespace std;

typedef int ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;

constexpr ll INF64 = 9000000000000000000, INF32 = 2000000000, MOD = 1000000007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;

random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());

template<typename T>
bool assign_max(T& a, T b) {
	if (b > a) {
		a = b;
		return true;
	}
	return false;
}

template<typename T>
bool assign_min(T& a, T b) {
	if (b < a) {
		a = b;
		return true;
	}
	return false;
}

template<typename T>
T square(T a) {
	return a * a;
}

template<>
struct std::hash<pair<ll, ll>> {
	ll operator() (pair<ll, ll> p) {
		return ((__int128)p.first * MOD + p.second) % INF64;
	}
};

struct segment_tree {
	vector<ll> pref;
	segment_tree(vector<ll> a = vector<ll>(0, 0)) {
        pref.push_back(0);
        for (auto i : a) {
            pref.push_back(pref.back() + i);
        }
	}
	ll get(ll l, ll r) {
		return pref[r] - pref[l];
	}
};

void solve() {
	ll n;
	cin >> n;
	vector<ll> pos(n);
	for (ll i = 0; i < n; i++) {
		ll x;
		cin >> x;
		x--;
		pos[x] = i;
	}
	vector<segment_tree> all;
	for (ll i = 0; i <= n; i++) {
		vector<ll> now(n);
		for (ll j = 0; j < n; j++) {
			now[j] = pos[j] > i;
		}
		all.emplace_back(now);
	}
	vector<ll> dp(n, INF32);
	dp[0] = 0;
	for (ll i = 1; i < n; i++) {
		ll now = 0;
		for (ll j = i - 1; j >= 0; j--) {
			now += all[pos[j + 1]].get(0, j + 1);
			//for (ll k = 0; k <= j; k++) {
			//	if (pos[k] > pos[j + 1]) {
			//		now++;
			//	}
			//}
			ll x = all[pos[j + 1]].get(j + 2, i + 1);
			now += x;
			now -= (i - j - 1) - x;
			//for (ll k = j + 2; k <= i; k++) {
			//	if (pos[k] > pos[j + 1]) {
			//		now++;
			//	} else {
			//		now--;
			//	}
			//}
			assign_min(dp[i], now + dp[j]);
		}
		now = 0;
		for (ll k = 0; k <= i; k++) {
			now += k - all[pos[k]].get(0, k);
			//for (ll l = 0; l < k; l++) {
			//	if (pos[k] > pos[l]) {
			//		now++;
			//	}
			//}
		}
		assign_min(dp[i], now);
	}
	cout << dp.back() << '\n';
}

int main() {
	if (IS_FILE) {
		freopen("", "r", stdin);
		freopen("", "w", stdout);
	}
    	ios_base::sync_with_stdio(false);
    	cin.tie(0);
    	cout.tie(0);
	ll t = 1;
	if (IS_TEST_CASES) {
		cin >> t;
	}
	for (ll i = 0; i < t; i++) {
		solve();
	}
}

Compilation message

Main.cpp:12:22: warning: overflow in conversion from 'long int' to 'll' {aka 'int'} changes value from '9000000000000000000' to '-494665728' [-Woverflow]
   12 | constexpr ll INF64 = 9000000000000000000, INF32 = 2000000000, MOD = 1000000007;
      |                      ^~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:120:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |   freopen("", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~
Main.cpp:121:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |   freopen("", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 5 ms 3540 KB Output is correct
32 Correct 5 ms 3540 KB Output is correct
33 Correct 5 ms 3540 KB Output is correct
34 Correct 5 ms 3540 KB Output is correct
35 Correct 6 ms 3540 KB Output is correct
36 Correct 5 ms 3540 KB Output is correct
37 Correct 5 ms 3540 KB Output is correct
38 Correct 5 ms 3540 KB Output is correct
39 Correct 5 ms 3540 KB Output is correct
40 Correct 7 ms 3540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 512 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 5 ms 3540 KB Output is correct
32 Correct 5 ms 3540 KB Output is correct
33 Correct 5 ms 3540 KB Output is correct
34 Correct 5 ms 3540 KB Output is correct
35 Correct 6 ms 3540 KB Output is correct
36 Correct 5 ms 3540 KB Output is correct
37 Correct 5 ms 3540 KB Output is correct
38 Correct 5 ms 3540 KB Output is correct
39 Correct 5 ms 3540 KB Output is correct
40 Correct 7 ms 3540 KB Output is correct
41 Correct 610 ms 118628 KB Output is correct
42 Correct 603 ms 118684 KB Output is correct
43 Correct 605 ms 118716 KB Output is correct
44 Correct 619 ms 118604 KB Output is correct
45 Correct 618 ms 118732 KB Output is correct
46 Correct 666 ms 118812 KB Output is correct
47 Correct 635 ms 118820 KB Output is correct
48 Correct 615 ms 118812 KB Output is correct
49 Correct 643 ms 118812 KB Output is correct
50 Correct 635 ms 118820 KB Output is correct
51 Correct 644 ms 118868 KB Output is correct
52 Correct 642 ms 118820 KB Output is correct
53 Correct 642 ms 118820 KB Output is correct
54 Correct 659 ms 118764 KB Output is correct
55 Correct 643 ms 118816 KB Output is correct