Submission #826640

#TimeUsernameProblemLanguageResultExecution timeMemory
826640jmyszka2007Collecting Stamps 3 (JOI20_ho_t3)C++17
0 / 100
1 ms468 KiB
#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() 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>rgt1(n + 1, vll(n + 1, 0)); vector<vll>rgt2(n + 1, vll(n + 1, 0)); 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++) { for(int j = i; j <= n; j++) { rgt1[i][tab[j]]++; if(tab[j] > tab[i]) { rgt2[i][tab[j]]++; } } for(int j = 1; j <= n; j++) { rgt1[i][j] += rgt1[i][j - 1]; sum1[j][tab[i]] += rgt1[i][j]; } for(int j = tab[i] + 1; j <= n; j++) { rgt2[i][j] += rgt2[i][j - 1]; sum2[j][tab[i]] += rgt2[i][j]; } debug(rgt2[i]); 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; auto get_sum1 = [&] (int x, int l, int r) { if(l > r) { return 0ll; } return sum1[x][r] - sum1[x][l - 1]; }; auto get_sum2 = [&] (int x, int l, int r) { if(l > r) { return 0ll; } return sum2[x][r] - sum2[x][l - 1]; }; auto get_sum3 = [&] (int l, int r) { if(l > r) { return 0ll; } return sum3[r] - sum3[l - 1]; }; debug(sum3); for(int i = 1; i <= n; i++) { for(int j = i; j >= 1; j--) { if(j == 1 && i == n) { debug(i, j); debug(get_sum1(j - 1, j, i)); debug(get_sum2(i, j, i)); debug(get_sum3(j, i) - ((i + 1 - j) * (j + i) / 2)); debug(dp[j - 1] + get_sum1(j - 1, j, i) + get_sum2(i, j, i) + get_sum3(j, i) - ((ll)(i + 1 - j) * (ll)(j + i) / 2)); } dp[i] = min(dp[i], dp[j - 1] + get_sum1(j - 1, j, i) + get_sum2(i, j, i) + get_sum3(j, i) - ((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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...