답안 #767733

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
767733 2023-06-27T06:07:55 Z drdilyor Ekoeko (COCI21_ekoeko) C++17
110 / 110
13 ms 4532 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct Fenwick {
    int n;
    vector<int> t;
    Fenwick (int n) : n(n), t(n) {}
    void inc(int i, int d=1) {
        for (; i < n; i |= i+1)
            t[i] += d;
    }
    int sum(int r) {
        int s = 0;
        for (; r >= 0; r = (r&(r+1))-1)
            s += t[r];
        return s;
    }
};

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int n;
    string s;
    cin >> n >> s;

    vector<vector<int>> ix(128);
    vector cnt(128, 0);

    for (char c : s) cnt[c]++;
    for (int i = 0; i < n*2; i++)
        ix[s[i]].push_back(i);

    vector left(n*2, 0);
    for (char c = 'a'; c <= 'z'; c++) {
        assert((int)ix[c].size() == cnt[c]);
        for (int i = 0; i < cnt[c]/2; i++) {
            left[ix[c][i]] = 1;
        }
    }

    vector<vector<int>> renum(128);
    vector arr(n*2, 0);
    for (int i = 0, k = 0; i < n * 2; i++) {
        if (left[i]) {
            arr[i] = k;
            renum[s[i]].push_back(k);
            k++;
        }
    }

    for (auto& i : renum) reverse(i.begin(), i.end());
    for (int i = 0; i < n * 2; i++) {
        if (!left[i]) {
            arr[i] = n + renum[s[i]].back();
            renum[s[i]].pop_back();
        }
    }

    Fenwick tree(n*2);
    ll res = 0;
    for (int i : arr) {
        res += tree.sum(n*2-1) - tree.sum(i);
        tree.inc(i);
    }
    cout << res << endl;

    return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 4 ms 1796 KB Output is correct
3 Correct 5 ms 3164 KB Output is correct
4 Correct 10 ms 4532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 4 ms 1796 KB Output is correct
3 Correct 5 ms 3164 KB Output is correct
4 Correct 10 ms 4532 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 4 ms 1796 KB Output is correct
3 Correct 5 ms 3164 KB Output is correct
4 Correct 10 ms 4532 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 10 ms 4460 KB Output is correct
7 Correct 9 ms 4332 KB Output is correct
8 Correct 13 ms 4324 KB Output is correct
9 Correct 12 ms 4308 KB Output is correct
10 Correct 12 ms 4328 KB Output is correct
11 Correct 11 ms 4200 KB Output is correct
12 Correct 11 ms 4260 KB Output is correct
13 Correct 10 ms 4168 KB Output is correct
14 Correct 12 ms 4184 KB Output is correct
15 Correct 9 ms 4060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 324 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 4 ms 1796 KB Output is correct
3 Correct 5 ms 3164 KB Output is correct
4 Correct 10 ms 4532 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 10 ms 4460 KB Output is correct
11 Correct 9 ms 4332 KB Output is correct
12 Correct 13 ms 4324 KB Output is correct
13 Correct 12 ms 4308 KB Output is correct
14 Correct 12 ms 4328 KB Output is correct
15 Correct 11 ms 4200 KB Output is correct
16 Correct 11 ms 4260 KB Output is correct
17 Correct 10 ms 4168 KB Output is correct
18 Correct 12 ms 4184 KB Output is correct
19 Correct 9 ms 4060 KB Output is correct
20 Correct 1 ms 256 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 320 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 324 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 320 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 324 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 10 ms 4432 KB Output is correct
33 Correct 9 ms 4320 KB Output is correct
34 Correct 13 ms 4356 KB Output is correct
35 Correct 12 ms 4368 KB Output is correct
36 Correct 13 ms 4320 KB Output is correct
37 Correct 12 ms 4192 KB Output is correct
38 Correct 11 ms 4252 KB Output is correct
39 Correct 11 ms 4192 KB Output is correct
40 Correct 10 ms 4156 KB Output is correct
41 Correct 9 ms 4160 KB Output is correct