답안 #492310

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
492310 2021-12-06T15:10:25 Z alextodoran Monochrome Points (JOI20_monochrome) C++17
100 / 100
977 ms 8072 KB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 200000;

int N;

string s;

vector <int> v[2];

int Fen[N_MAX * 2 + 2];

void update (int x) {
    x++;
    for (int i = x; i <= N * 2; i += i & -i) {
        Fen[i]++;
    }
}
int query (int x) {
    x++;
    int cnt = 0;
    for (int i = x; i >= 1; i -= i & -i) {
        cnt += Fen[i];
    }
    return cnt;
}
int intersections (int x, int y) {
    return query(y) - query(x);
}

ll solve (int offs) {
    for (int i = 1; i <= N * 2; i++) {
        Fen[i] = 0;
    }
    vector <pair <int, int>> segments;
    for (int i = 0; i < N; i++) {
        int j = (i + offs) % N;
        segments.push_back(make_pair(v[0][i], v[1][j]));
        if (segments.back().first > segments.back().second) {
            swap(segments.back().first, segments.back().second);
        }
    }
    sort(segments.begin(), segments.end());
    ll cnt = 0;
    for (pair <int, int> p : segments) {
        cnt += intersections(p.first, p.second);
        update(p.second);
    }
    return cnt;
}

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

    cin >> N;
    cin >> s;

    for (int i = 0; i < N * 2; i++) {
        v[(s[i] == 'B')].push_back(i);
    }

    int l = 0, r = N - 1;
    while (l < r) {
        int mid = (l + r) / 2;
        if (solve(mid) < solve(mid + 1)) {
            l = mid + 1;
        } else {
            r = mid;
        }
    }

    cout << solve(l) << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 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 1 ms 332 KB Output is correct
17 Correct 1 ms 332 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 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 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 1 ms 332 KB Output is correct
17 Correct 1 ms 332 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 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 4 ms 396 KB Output is correct
35 Correct 4 ms 332 KB Output is correct
36 Correct 4 ms 332 KB Output is correct
37 Correct 4 ms 332 KB Output is correct
38 Correct 3 ms 332 KB Output is correct
39 Correct 3 ms 332 KB Output is correct
40 Correct 3 ms 392 KB Output is correct
41 Correct 3 ms 332 KB Output is correct
42 Correct 4 ms 332 KB Output is correct
43 Correct 4 ms 332 KB Output is correct
44 Correct 4 ms 332 KB Output is correct
45 Correct 4 ms 332 KB Output is correct
46 Correct 4 ms 388 KB Output is correct
47 Correct 3 ms 332 KB Output is correct
48 Correct 3 ms 332 KB Output is correct
49 Correct 3 ms 332 KB Output is correct
50 Correct 4 ms 388 KB Output is correct
51 Correct 4 ms 332 KB Output is correct
52 Correct 4 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 0 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 1 ms 332 KB Output is correct
17 Correct 1 ms 332 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 204 KB Output is correct
26 Correct 1 ms 204 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Correct 1 ms 204 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 204 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 4 ms 396 KB Output is correct
35 Correct 4 ms 332 KB Output is correct
36 Correct 4 ms 332 KB Output is correct
37 Correct 4 ms 332 KB Output is correct
38 Correct 3 ms 332 KB Output is correct
39 Correct 3 ms 332 KB Output is correct
40 Correct 3 ms 392 KB Output is correct
41 Correct 3 ms 332 KB Output is correct
42 Correct 4 ms 332 KB Output is correct
43 Correct 4 ms 332 KB Output is correct
44 Correct 4 ms 332 KB Output is correct
45 Correct 4 ms 332 KB Output is correct
46 Correct 4 ms 388 KB Output is correct
47 Correct 3 ms 332 KB Output is correct
48 Correct 3 ms 332 KB Output is correct
49 Correct 3 ms 332 KB Output is correct
50 Correct 4 ms 388 KB Output is correct
51 Correct 4 ms 332 KB Output is correct
52 Correct 4 ms 332 KB Output is correct
53 Correct 905 ms 7612 KB Output is correct
54 Correct 885 ms 8016 KB Output is correct
55 Correct 909 ms 7664 KB Output is correct
56 Correct 830 ms 7680 KB Output is correct
57 Correct 729 ms 7716 KB Output is correct
58 Correct 664 ms 7844 KB Output is correct
59 Correct 714 ms 7656 KB Output is correct
60 Correct 804 ms 7760 KB Output is correct
61 Correct 889 ms 7732 KB Output is correct
62 Correct 806 ms 7740 KB Output is correct
63 Correct 903 ms 7816 KB Output is correct
64 Correct 674 ms 7788 KB Output is correct
65 Correct 829 ms 7488 KB Output is correct
66 Correct 803 ms 7656 KB Output is correct
67 Correct 861 ms 7632 KB Output is correct
68 Correct 588 ms 7360 KB Output is correct
69 Correct 691 ms 8072 KB Output is correct
70 Correct 652 ms 7360 KB Output is correct
71 Correct 911 ms 7920 KB Output is correct
72 Correct 561 ms 7872 KB Output is correct
73 Correct 977 ms 7880 KB Output is correct