Submission #1048184

# Submission time Handle Problem Language Result Execution time Memory
1048184 2024-08-08T04:25:24 Z thangdz2k7 Line Town (CCO23_day1problem3) C++17
0 / 25
1 ms 348 KB
#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 time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -