Submission #767591

# Submission time Handle Problem Language Result Execution time Memory
767591 2023-06-26T21:23:43 Z PurpleCrayon Chorus (JOI23_chorus) C++17
0 / 100
0 ms 340 KB
#include <bits/stdc++.h>
using namespace std;
 
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 1e6+10, MOD = 1e9+7;
const ll INF = 1e18+10;
 
int n, K, small[N];
ll ps[N], l_use[N];
vector<int> one, two;
 
struct Line {
    ll m, b;
    int id;
    ll calc(ll x) {
        return m * x + b;
    }
};
 
struct CHT {
    deque<Line> lines;
 
    void add(ll m, ll b, int id) {
        while (sz(lines) >= 2) {
            auto A = lines.end()[-2];
            auto B = lines.end()[-1];

            if ((b - A.b) * (A.m - B.m) <= (B.b - A.b) * (A.m - m)) lines.pop_back();
            else break;
        }
        lines.push_back({m, b, id});
    }
 
    Line qry(ll x) {
        while (sz(lines) >= 2 && lines[0].calc(x) >= lines[1].calc(x)) {
            lines.pop_front();
        }
 
        return lines[0];
    }
};
 
pair<ll, int> calc(ll d) {
    vector<pair<ll, int>> dp(n);
 
    CHT lines;
    for (int i = 0; i < n; i++) {
        lines.add(-i, (long long) i * i - i + l_use[i] + (i == 0 ? 0 : dp[i-1].first), i);
        auto line = lines.qry(i);
        dp[i].first = line.calc(i) + ps[i] + d;
        dp[i].second = line.id ? dp[line.id-1].second + 1 : 1;

        int l = small[i];
        if (l <= i) {
            if (l == 0) dp[i] = min(dp[i], make_pair(d + 0LL, 1));
            else dp[i] = min(dp[i], make_pair(d + dp[l-1].first, dp[l-1].second + 1));
        }
    }

    return dp[n-1];
}

void solve() {
    cin >> n >> K;
    for (int i = 0; i < 2 * n; i++) {
        char c; cin >> c;
        if (c == 'A') one.push_back(i);
        else two.push_back(i);
    }
 
    for (int i = 0; i < n; i++) {
        small[i] = lower_bound(two.begin(), two.end(), one[i]) - two.begin();
    }
 
    for (int i = 0; i < n; i++) {
        ps[i] = small[i] + (i ? ps[i-1] : 0);
    }

    for (int i = 0; i < n; i++) {
        l_use[i] = -(i ? ps[i-1] : 0);
        int use = lower_bound(small, small + n, i) - small - 1; // [i..use]
        if (use >= i) {
            l_use[i] -= ps[use] - (i ? ps[i-1] : 0) - (long long) i * (use - i + 1);
        }
    }
 
    // cerr << calc(0).first << ' ' << calc(0).second << endl;
    ll lo = -1, hi = (long long) 2e12, mid = (lo + hi) / 2;
    while (lo < mid && mid < hi) {
        auto [cost, cnt] = calc(mid);
        if (cnt <= K) hi = mid;
        else lo = mid;
        mid = (lo + hi) / 2;
    }

    auto [cost, cnt] = calc(hi);
    // cerr << lo << ' ' << cost << ' ' << cnt << endl;
    cout << cost - hi * cnt << '\n';
}
 
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Incorrect 0 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Incorrect 0 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Incorrect 0 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Incorrect 0 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Incorrect 0 ms 340 KB Output isn't correct
7 Halted 0 ms 0 KB -