Submission #850477

# Submission time Handle Problem Language Result Execution time Memory
850477 2023-09-16T15:48:29 Z serifefedartar Ekoeko (COCI21_ekoeko) C++17
110 / 110
22 ms 5740 KB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll LOGN = 20;
const ll MAXN = 2e5 + 5;

vector<int> occ[26];
vector<int> leftV, rightV;
ll fen[MAXN];
void update(int k) {
    while (k < MAXN) {
        fen[k]++;
        k += k&-k;
    }
}

ll query(int k) {
    ll res = 0;
    while (k >= 1) {
        res += fen[k];
        k -= k&-k;
    }
    return res;
}

int main() {
    fast
    int n;
    string s;
    cin >> n >> s;
    s = "#" + s;

    for (int i = 1; i <= 2*n; i++)
        occ[s[i]-'a'].push_back(i);

    for (int i = 0; i < 26; i++) {
        int q = occ[i].size() / 2;
        for (int j = 0; j < q; j++)
            leftV.push_back(occ[i][j]);
        for (int j = q; j < 2*q; j++)
            rightV.push_back(occ[i][j]);
    }

    sort(leftV.begin(), leftV.end());
    sort(rightV.begin(), rightV.end());
    
    vector<int> occ2[26];
    for (int i = n-1; i >= 0; i--) {
        occ2[s[leftV[i]] - 'a'].push_back(i+1);
    }

    vector<int> inv;
    for (auto u : rightV) {
        int ch = s[u] - 'a';
        inv.push_back(occ2[ch].back());
        occ2[ch].pop_back();
    }

    ll ans = 0;
    for (int i = 0; i < n; i++) {
        ans += i - query(inv[i]);
        update(inv[i]);
    }

    memset(fen, 0, sizeof(fen));
    for (auto u : rightV)
        leftV.push_back(u);

    for (int i = 0; i < 2*n; i++) {
        ans += i - query(leftV[i]);
        update(leftV[i]);
    }

    cout << ans << "\n"; 
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1880 KB Output is correct
2 Correct 5 ms 3420 KB Output is correct
3 Correct 8 ms 4968 KB Output is correct
4 Correct 12 ms 5672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1880 KB Output is correct
2 Correct 5 ms 3420 KB Output is correct
3 Correct 8 ms 4968 KB Output is correct
4 Correct 12 ms 5672 KB Output is correct
5 Correct 1 ms 1880 KB Output is correct
6 Correct 1 ms 1880 KB Output is correct
7 Correct 1 ms 1880 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1880 KB Output is correct
2 Correct 5 ms 3420 KB Output is correct
3 Correct 8 ms 4968 KB Output is correct
4 Correct 12 ms 5672 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 19 ms 5312 KB Output is correct
7 Correct 18 ms 5740 KB Output is correct
8 Correct 18 ms 5556 KB Output is correct
9 Correct 18 ms 5556 KB Output is correct
10 Correct 19 ms 5560 KB Output is correct
11 Correct 18 ms 5576 KB Output is correct
12 Correct 19 ms 5440 KB Output is correct
13 Correct 18 ms 5576 KB Output is correct
14 Correct 17 ms 5584 KB Output is correct
15 Correct 17 ms 5584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1884 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 2000 KB Output is correct
5 Correct 1 ms 1884 KB Output is correct
6 Correct 1 ms 1880 KB Output is correct
7 Correct 1 ms 1884 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 1880 KB Output is correct
11 Correct 1 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1880 KB Output is correct
2 Correct 5 ms 3420 KB Output is correct
3 Correct 8 ms 4968 KB Output is correct
4 Correct 12 ms 5672 KB Output is correct
5 Correct 1 ms 1880 KB Output is correct
6 Correct 1 ms 1880 KB Output is correct
7 Correct 1 ms 1880 KB Output is correct
8 Correct 1 ms 1884 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 19 ms 5312 KB Output is correct
11 Correct 18 ms 5740 KB Output is correct
12 Correct 18 ms 5556 KB Output is correct
13 Correct 18 ms 5556 KB Output is correct
14 Correct 19 ms 5560 KB Output is correct
15 Correct 18 ms 5576 KB Output is correct
16 Correct 19 ms 5440 KB Output is correct
17 Correct 18 ms 5576 KB Output is correct
18 Correct 17 ms 5584 KB Output is correct
19 Correct 17 ms 5584 KB Output is correct
20 Correct 1 ms 1884 KB Output is correct
21 Correct 1 ms 1884 KB Output is correct
22 Correct 1 ms 1884 KB Output is correct
23 Correct 1 ms 2000 KB Output is correct
24 Correct 1 ms 1884 KB Output is correct
25 Correct 1 ms 1880 KB Output is correct
26 Correct 1 ms 1884 KB Output is correct
27 Correct 1 ms 1884 KB Output is correct
28 Correct 1 ms 1884 KB Output is correct
29 Correct 1 ms 1880 KB Output is correct
30 Correct 1 ms 1884 KB Output is correct
31 Correct 1 ms 1880 KB Output is correct
32 Correct 22 ms 5552 KB Output is correct
33 Correct 20 ms 5464 KB Output is correct
34 Correct 19 ms 5572 KB Output is correct
35 Correct 18 ms 5552 KB Output is correct
36 Correct 20 ms 5560 KB Output is correct
37 Correct 18 ms 5572 KB Output is correct
38 Correct 17 ms 5384 KB Output is correct
39 Correct 17 ms 5580 KB Output is correct
40 Correct 17 ms 5584 KB Output is correct
41 Correct 17 ms 5472 KB Output is correct