Submission #767802

# Submission time Handle Problem Language Result Execution time Memory
767802 2023-06-27T07:42:49 Z dxz05 Ekoeko (COCI21_ekoeko) C++17
110 / 110
15 ms 4416 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair
#define BIT(x, i) (((x) >> (i)) & 1)
//#define endl '\n'

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

typedef long long ll;
const int MOD = 1e9 + 7;
const int N = 1e6 + 3e2;

ll calc(const string &s, const string &t){
    int n = (int) s.size();

    vector<queue<int>> q(26);
    for (int i = 0; i < n; i++) q[s[i] - 'a'].push(i);

    vector<int> a(n);
    for (int i = 0; i < n; i++){
        a[i] = q[t[i] - 'a'].front();
        q[t[i] - 'a'].pop();
    }

    ll inv = 0;

    vector<int> f(n + 1);
    for (int i = n - 1; i >= 0; i--){
        int x = a[i] + 1;

        while (x > 0) inv += f[x], x -= (-x) & x;

        x = a[i] + 1;
        while (x <= n) f[x]++, x += (-x) & x;
    }

    return inv;
}

void solve(){
    int n; string s;
    cin >> n >> s;

    vector<vector<int>> pos(26);
    for (int i = 0; i < 2 * n; i++){
        pos[s[i] - 'a'].push_back(i);
    }

    string a, b;
    vector<int> p(26, 0);

    for (int i = 0; i < 2 * n; i++){
        int x = s[i] - 'a';
        while (pos[x][p[x]] != i) p[x]++;

        if (p[x] * 2 < (int) pos[x].size()){
            a += s[i];
        } else {
            b += s[i];
        }
    }

    cout << calc(s, a + b) + calc(a, b) << endl;

}

int main(){
    clock_t startTime = clock();
    ios_base::sync_with_stdio(false);

#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int test_cases = 1;
//    cin >> test_cases;

    for (int test = 1; test <= test_cases; test++){
        // cout << (solve() ? "YES" : "NO") << endl;
        solve();
    }

#ifdef LOCAL
    cerr << "Time: " << int((double) (clock() - startTime) / CLOCKS_PER_SEC * 1000) << " ms" << endl;
#endif

    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:74:13: warning: unused variable 'startTime' [-Wunused-variable]
   74 |     clock_t startTime = clock();
      |             ^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 4 ms 1680 KB Output is correct
3 Correct 9 ms 2908 KB Output is correct
4 Correct 11 ms 4312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 4 ms 1680 KB Output is correct
3 Correct 9 ms 2908 KB Output is correct
4 Correct 11 ms 4312 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 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 4 ms 1680 KB Output is correct
3 Correct 9 ms 2908 KB Output is correct
4 Correct 11 ms 4312 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 11 ms 4416 KB Output is correct
7 Correct 11 ms 4320 KB Output is correct
8 Correct 13 ms 4320 KB Output is correct
9 Correct 12 ms 4316 KB Output is correct
10 Correct 12 ms 4196 KB Output is correct
11 Correct 10 ms 4192 KB Output is correct
12 Correct 15 ms 4236 KB Output is correct
13 Correct 11 ms 4212 KB Output is correct
14 Correct 12 ms 4064 KB Output is correct
15 Correct 10 ms 4064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 328 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 4 ms 1680 KB Output is correct
3 Correct 9 ms 2908 KB Output is correct
4 Correct 11 ms 4312 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 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 11 ms 4416 KB Output is correct
11 Correct 11 ms 4320 KB Output is correct
12 Correct 13 ms 4320 KB Output is correct
13 Correct 12 ms 4316 KB Output is correct
14 Correct 12 ms 4196 KB Output is correct
15 Correct 10 ms 4192 KB Output is correct
16 Correct 15 ms 4236 KB Output is correct
17 Correct 11 ms 4212 KB Output is correct
18 Correct 12 ms 4064 KB Output is correct
19 Correct 10 ms 4064 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 328 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 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 11 ms 4316 KB Output is correct
33 Correct 11 ms 4320 KB Output is correct
34 Correct 11 ms 4300 KB Output is correct
35 Correct 11 ms 4320 KB Output is correct
36 Correct 12 ms 4192 KB Output is correct
37 Correct 13 ms 4192 KB Output is correct
38 Correct 10 ms 4188 KB Output is correct
39 Correct 11 ms 4192 KB Output is correct
40 Correct 10 ms 4064 KB Output is correct
41 Correct 10 ms 4060 KB Output is correct