Submission #827218

# Submission time Handle Problem Language Result Execution time Memory
827218 2023-08-16T09:59:47 Z jmyszka2007 Group Photo (JOI21_ho_t3) C++17
100 / 100
1060 ms 392352 KB
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
template<class A, class B>
ostream& operator<<(ostream& o, const pair<A, B>& p) {return o << '(' << p.first << ", " << p.second << ')';}
template<size_t Index = 0, typename... Types>
ostream& printTupleElements(ostream& o, const tuple<Types...>& t) {if constexpr (Index < sizeof...(Types)){if(Index > 0){o << ", ";}o << get<Index>(t);printTupleElements<Index + 1>(o, t);}return o;}
template<typename... Types>
ostream& operator<<(ostream& o, const tuple<Types...>& t){o << "(";printTupleElements(o, t);return o << ")";}
template<class T>
auto operator<<(ostream& o, const T& x) -> decltype(x.end(), o){o << '{';bool first = true;for (const auto& e : x){if (!first){o << ", ";}o << e;first = false;} return o << '}';}
//#define DEBUG
#ifdef DEBUG
#define fastio()
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#define check(x) if (!(x)) { cerr << "Check failed: " << #x << " in line " << __LINE__ << endl; exit(1); }
#else
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0);
#define debug(...)
#define check(x) 
#endif
typedef long long ll;
#define pi pair<int, int>
#define pl pair<ll, ll>
#define st first
#define nd second
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
ll get_sum(vector<vll>sum, int x, int l, int r) {
	if(l > r) {
		return 0ll;
	}
	return sum[x][r] - sum[x][l - 1];
}
ll get_sum2(vll sum, int l, int r) {
	if(l > r) {
		return 0ll;
	}
	return sum[r] - sum[l - 1];
}
void solve() {
	//ifstream cin("nazwa.in");
	//ofstream cout("nazwa.out");
	int n;
	cin >> n;
	vi tab(n + 1), pos(n + 1);
	for(int i = 1; i <= n; i++) {
		cin >> tab[i];
		pos[tab[i]] = i;
	}
	vector<vll>sum1(n + 1, vll(n + 1, 0));
	vector<vll>sum2(n + 1, vll(n + 1, 0));
	vll sum3(n + 1, 0);
	for(int i = 1; i <= n; i++) {
		vll rgt1(n + 1, 0), rgt2(n + 1, 0);
		for(int j = i; j <= n; j++) {
			rgt1[tab[j]]++;
			if(tab[j] > tab[i]) {
				rgt2[tab[j]]++;
			}
		}
		for(int j = 1; j <= n; j++) {
			rgt1[j] += rgt1[j - 1];
			sum1[j][tab[i]] += rgt1[j];
		}
		for(int j = tab[i] + 1; j <= n; j++) {
			rgt2[j] += rgt2[j - 1];
			sum2[j][tab[i]] += rgt2[j];
		}
		sum3[tab[i]] += i;
	}
	debug(sum2[n]);
	for(int j = 1; j <= n; j++) {
		for(int i = 1; i <= n; i++) {
			sum1[j][i] += sum1[j][i - 1];
			sum2[j][i] += sum2[j][i - 1];
		}
		sum3[j] += sum3[j - 1];
	}
	vll dp(n + 1, 1e18);
	dp[0] = 0;
	for(int i = 1; i <= n; i++) {
		for(int j = i; j >= 1; j--) {
			dp[i] = min(dp[i], dp[j - 1] + (sum1[j - 1][i] - sum1[j - 1][j - 1]) + (sum2[i][i] - sum2[i][j - 1]) + (sum3[i] - sum3[j - 1]) - ((ll)(i + 1 - j) * (ll)(j + i) / 2));
		}
	}
	debug(dp);
	cout << dp[n] << '\n';
}
int main() {
	fastio();
	int t;
	//cin >> t;
	t = 1;	
	while(t--) {
		solve();
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 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 288 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 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 1 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 288 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 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 216 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 216 KB Output is correct
15 Correct 0 ms 216 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 220 KB Output is correct
19 Correct 0 ms 220 KB Output is correct
20 Correct 1 ms 220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 288 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 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 216 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 216 KB Output is correct
15 Correct 0 ms 216 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 220 KB Output is correct
19 Correct 0 ms 220 KB Output is correct
20 Correct 1 ms 220 KB Output is correct
21 Correct 1 ms 856 KB Output is correct
22 Correct 1 ms 864 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 864 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 864 KB Output is correct
29 Correct 1 ms 856 KB Output is correct
30 Correct 1 ms 864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 288 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 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 216 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 216 KB Output is correct
15 Correct 0 ms 216 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 220 KB Output is correct
19 Correct 0 ms 220 KB Output is correct
20 Correct 1 ms 220 KB Output is correct
21 Correct 1 ms 856 KB Output is correct
22 Correct 1 ms 864 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 864 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 864 KB Output is correct
29 Correct 1 ms 856 KB Output is correct
30 Correct 1 ms 864 KB Output is correct
31 Correct 13 ms 10336 KB Output is correct
32 Correct 13 ms 10440 KB Output is correct
33 Correct 14 ms 10328 KB Output is correct
34 Correct 13 ms 10328 KB Output is correct
35 Correct 15 ms 10336 KB Output is correct
36 Correct 12 ms 10332 KB Output is correct
37 Correct 12 ms 10436 KB Output is correct
38 Correct 15 ms 10336 KB Output is correct
39 Correct 13 ms 10324 KB Output is correct
40 Correct 20 ms 10440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 288 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 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 216 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 216 KB Output is correct
15 Correct 0 ms 216 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 220 KB Output is correct
19 Correct 0 ms 220 KB Output is correct
20 Correct 1 ms 220 KB Output is correct
21 Correct 1 ms 856 KB Output is correct
22 Correct 1 ms 864 KB Output is correct
23 Correct 1 ms 860 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 1 ms 864 KB Output is correct
27 Correct 1 ms 860 KB Output is correct
28 Correct 1 ms 864 KB Output is correct
29 Correct 1 ms 856 KB Output is correct
30 Correct 1 ms 864 KB Output is correct
31 Correct 13 ms 10336 KB Output is correct
32 Correct 13 ms 10440 KB Output is correct
33 Correct 14 ms 10328 KB Output is correct
34 Correct 13 ms 10328 KB Output is correct
35 Correct 15 ms 10336 KB Output is correct
36 Correct 12 ms 10332 KB Output is correct
37 Correct 12 ms 10436 KB Output is correct
38 Correct 15 ms 10336 KB Output is correct
39 Correct 13 ms 10324 KB Output is correct
40 Correct 20 ms 10440 KB Output is correct
41 Correct 978 ms 391924 KB Output is correct
42 Correct 984 ms 392024 KB Output is correct
43 Correct 995 ms 392140 KB Output is correct
44 Correct 972 ms 392260 KB Output is correct
45 Correct 928 ms 392264 KB Output is correct
46 Correct 930 ms 392348 KB Output is correct
47 Correct 1029 ms 392344 KB Output is correct
48 Correct 980 ms 392340 KB Output is correct
49 Correct 1060 ms 392348 KB Output is correct
50 Correct 959 ms 392352 KB Output is correct
51 Correct 1002 ms 392340 KB Output is correct
52 Correct 975 ms 392344 KB Output is correct
53 Correct 979 ms 392340 KB Output is correct
54 Correct 996 ms 392344 KB Output is correct
55 Correct 943 ms 392340 KB Output is correct