답안 #387546

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
387546 2021-04-08T22:54:19 Z timmyfeng Monochrome Points (JOI20_monochrome) C++17
100 / 100
1384 ms 6508 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 200000;

template <class T>
struct fenwick {
    vector<T> tree;
    int n;

    fenwick(int n) : n(n) {
        tree.resize(n + 1);
    }

    void update(int a, T x) {
        for ( ; a <= n; a += (a & -a)) {
            tree[a] += x;
        }
    }

    T query(int a) {
        T res = 0;
        for ( ; a > 0; a -= (a & -a)) {
            res += tree[a];
        }
        return res;
    }

    int lower_bound(T k) {
        int res = 0;
        T sum = 0;
        for (int i = __lg(n); i >= 0; --i) {
            if (res + (1 << i) <= n && sum + tree[res + (1 << i)] < k) {
                res += 1 << i;
                sum += tree[res];
            }
        }
        return res + 1;
    }
};

int black[N], white[N], n;

long long calc(int k) {
    vector<array<int, 2>> segments;
    for (int i = 0; i < n; ++i) {
        int b = black[i], w = white[(i + k) % n];
        if (b > w) {
            swap(b, w);
        }
        segments.push_back({b, w});
    }
    sort(segments.begin(), segments.end());

    long long ans = 0;
    fenwick<int> fenw(2 * n);
    for (auto [l, r] : segments) {
        ans += fenw.query(r) - fenw.query(l);
        fenw.update(r, 1);
    }

    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    cin >> n >> s;

    int b = 0, w = 0;
    for (int i = 0; i < 2 * n; ++i) {
        if (s[i] == 'B') {
            black[b++] = i;
        } else {
            white[w++] = i;
        }
    }

    int low = 0, high = n - 1;
    while (low < high) {
        int mid = (low + high) / 2;
        if (calc(mid) < calc(mid + 1)) {
            low = mid + 1;
        } else {
            high = mid;
        }
    }

    cout << calc(low) << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 232 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 232 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 232 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 324 KB Output is correct
34 Correct 5 ms 332 KB Output is correct
35 Correct 5 ms 332 KB Output is correct
36 Correct 6 ms 332 KB Output is correct
37 Correct 5 ms 332 KB Output is correct
38 Correct 5 ms 332 KB Output is correct
39 Correct 5 ms 400 KB Output is correct
40 Correct 4 ms 332 KB Output is correct
41 Correct 4 ms 388 KB Output is correct
42 Correct 5 ms 332 KB Output is correct
43 Correct 5 ms 332 KB Output is correct
44 Correct 5 ms 396 KB Output is correct
45 Correct 5 ms 332 KB Output is correct
46 Correct 5 ms 332 KB Output is correct
47 Correct 5 ms 332 KB Output is correct
48 Correct 6 ms 332 KB Output is correct
49 Correct 4 ms 332 KB Output is correct
50 Correct 5 ms 460 KB Output is correct
51 Correct 7 ms 320 KB Output is correct
52 Correct 6 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 232 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 324 KB Output is correct
34 Correct 5 ms 332 KB Output is correct
35 Correct 5 ms 332 KB Output is correct
36 Correct 6 ms 332 KB Output is correct
37 Correct 5 ms 332 KB Output is correct
38 Correct 5 ms 332 KB Output is correct
39 Correct 5 ms 400 KB Output is correct
40 Correct 4 ms 332 KB Output is correct
41 Correct 4 ms 388 KB Output is correct
42 Correct 5 ms 332 KB Output is correct
43 Correct 5 ms 332 KB Output is correct
44 Correct 5 ms 396 KB Output is correct
45 Correct 5 ms 332 KB Output is correct
46 Correct 5 ms 332 KB Output is correct
47 Correct 5 ms 332 KB Output is correct
48 Correct 6 ms 332 KB Output is correct
49 Correct 4 ms 332 KB Output is correct
50 Correct 5 ms 460 KB Output is correct
51 Correct 7 ms 320 KB Output is correct
52 Correct 6 ms 332 KB Output is correct
53 Correct 1384 ms 6352 KB Output is correct
54 Correct 1282 ms 6400 KB Output is correct
55 Correct 1201 ms 6220 KB Output is correct
56 Correct 1303 ms 6216 KB Output is correct
57 Correct 863 ms 6208 KB Output is correct
58 Correct 972 ms 6404 KB Output is correct
59 Correct 1100 ms 6196 KB Output is correct
60 Correct 1074 ms 6176 KB Output is correct
61 Correct 1241 ms 6224 KB Output is correct
62 Correct 1139 ms 6228 KB Output is correct
63 Correct 1273 ms 6224 KB Output is correct
64 Correct 1102 ms 6272 KB Output is correct
65 Correct 1200 ms 6092 KB Output is correct
66 Correct 839 ms 6128 KB Output is correct
67 Correct 854 ms 6232 KB Output is correct
68 Correct 1074 ms 6012 KB Output is correct
69 Correct 960 ms 6344 KB Output is correct
70 Correct 1103 ms 6048 KB Output is correct
71 Correct 809 ms 6352 KB Output is correct
72 Correct 1358 ms 6400 KB Output is correct
73 Correct 1375 ms 6508 KB Output is correct