답안 #767598

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
767598 2023-06-26T22:27:48 Z PurpleCrayon Chorus (JOI23_chorus) C++17
100 / 100
1349 ms 57580 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 << cost << ' ' << hi << ' ' << cnt << endl;
    // cerr << calc(hi-1).first << ' ' << calc(hi-1).second << endl;
    // cerr << lo << ' ' << cost << ' ' << cnt << endl;
    cout << cost - hi * K << '\n';
}
 
int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 4 ms 596 KB Output is correct
30 Correct 5 ms 596 KB Output is correct
31 Correct 5 ms 596 KB Output is correct
32 Correct 3 ms 596 KB Output is correct
33 Correct 3 ms 596 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 4 ms 596 KB Output is correct
36 Correct 5 ms 596 KB Output is correct
37 Correct 5 ms 596 KB Output is correct
38 Correct 4 ms 596 KB Output is correct
39 Correct 5 ms 596 KB Output is correct
40 Correct 4 ms 596 KB Output is correct
41 Correct 4 ms 596 KB Output is correct
42 Correct 4 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 4 ms 596 KB Output is correct
30 Correct 5 ms 596 KB Output is correct
31 Correct 5 ms 596 KB Output is correct
32 Correct 3 ms 596 KB Output is correct
33 Correct 3 ms 596 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 4 ms 596 KB Output is correct
36 Correct 5 ms 596 KB Output is correct
37 Correct 5 ms 596 KB Output is correct
38 Correct 4 ms 596 KB Output is correct
39 Correct 5 ms 596 KB Output is correct
40 Correct 4 ms 596 KB Output is correct
41 Correct 4 ms 596 KB Output is correct
42 Correct 4 ms 596 KB Output is correct
43 Correct 63 ms 3592 KB Output is correct
44 Correct 109 ms 5896 KB Output is correct
45 Correct 108 ms 5928 KB Output is correct
46 Correct 59 ms 6068 KB Output is correct
47 Correct 70 ms 5924 KB Output is correct
48 Correct 71 ms 5932 KB Output is correct
49 Correct 67 ms 5928 KB Output is correct
50 Correct 105 ms 5928 KB Output is correct
51 Correct 108 ms 5828 KB Output is correct
52 Correct 86 ms 5928 KB Output is correct
53 Correct 93 ms 5928 KB Output is correct
54 Correct 112 ms 5892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 4 ms 596 KB Output is correct
30 Correct 5 ms 596 KB Output is correct
31 Correct 5 ms 596 KB Output is correct
32 Correct 3 ms 596 KB Output is correct
33 Correct 3 ms 596 KB Output is correct
34 Correct 4 ms 596 KB Output is correct
35 Correct 4 ms 596 KB Output is correct
36 Correct 5 ms 596 KB Output is correct
37 Correct 5 ms 596 KB Output is correct
38 Correct 4 ms 596 KB Output is correct
39 Correct 5 ms 596 KB Output is correct
40 Correct 4 ms 596 KB Output is correct
41 Correct 4 ms 596 KB Output is correct
42 Correct 4 ms 596 KB Output is correct
43 Correct 63 ms 3592 KB Output is correct
44 Correct 109 ms 5896 KB Output is correct
45 Correct 108 ms 5928 KB Output is correct
46 Correct 59 ms 6068 KB Output is correct
47 Correct 70 ms 5924 KB Output is correct
48 Correct 71 ms 5932 KB Output is correct
49 Correct 67 ms 5928 KB Output is correct
50 Correct 105 ms 5928 KB Output is correct
51 Correct 108 ms 5828 KB Output is correct
52 Correct 86 ms 5928 KB Output is correct
53 Correct 93 ms 5928 KB Output is correct
54 Correct 112 ms 5892 KB Output is correct
55 Correct 787 ms 36460 KB Output is correct
56 Correct 1031 ms 57436 KB Output is correct
57 Correct 1211 ms 57368 KB Output is correct
58 Correct 649 ms 57452 KB Output is correct
59 Correct 758 ms 57580 KB Output is correct
60 Correct 800 ms 57572 KB Output is correct
61 Correct 773 ms 57556 KB Output is correct
62 Correct 1222 ms 57424 KB Output is correct
63 Correct 1349 ms 57372 KB Output is correct
64 Correct 1012 ms 57460 KB Output is correct
65 Correct 926 ms 57496 KB Output is correct
66 Correct 1277 ms 57440 KB Output is correct
67 Correct 906 ms 57524 KB Output is correct