Submission #1048184

#TimeUsernameProblemLanguageResultExecution timeMemory
1048184thangdz2k7Line Town (CCO23_day1problem3)C++17
0 / 25
1 ms348 KiB
#include <bits/stdc++.h> using namespace std; const int N = 2e3 + 5; int n, a[N], sr[N]; int dp[N][N]; void solve(){ cin >> n; for (int i = 1; i <= n; ++ i) cin >> a[i], sr[i] = i; sort(sr + 1, sr + n + 1, [&](int u, int v){ return abs(a[u]) > abs(a[v]); }); for (int i = 0; i <= n; ++ i) for (int j = 0; j <= n; ++ j) dp[i][j] = 1e9; dp[0][0] = 0; for (int i = 0; i < n; ++ i){ int id = sr[i + 1]; //cout << id << endl; for (int j = 0; j <= i; ++ j){ int l = j + 1; int r = n - (i - j); //if (i == 1 && j == 0) cout << l << ' ' << r << ' ' << id << endl; int tus = ((l ^ id) & 1) ^ (a[id] < 0); if (a[id] == 0) tus = 1; if (tus) dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + abs(id - l)); tus = ((r ^ id) & 1) ^ (a[id] > 0); if (a[id] == 0) tus = 1; if (tus) dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + abs(id - r)); } } //cout << dp[2][0] << endl; int ans = 1e9; for (int i = 0; i <= n; ++ i) ans = min(ans, dp[n][i]); if (ans == 1e9) cout << -1; else cout << ans / 2; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); 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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...