Submission #677074

# Submission time Handle Problem Language Result Execution time Memory
677074 2023-01-02T08:36:46 Z SamNguyen Ekoeko (COCI21_ekoeko) C++14
110 / 110
10 ms 3476 KB
#include <bits/stdc++.h>
using namespace std;

#define INPFILE "krokro.inp"
#define OUTFILE "krokro.out"
using lli = long long;

template <int N>
class BIT {
private:
    int n, bit[N + 7];

public:
    void init(int _n) {
        n = _n;
        memset(bit, 0, sizeof bit);
    }

    inline void add(int i, int v) {
        for (; i <= n; i += i & -i) bit[i] += v;
    }

    inline int get(int i) {
        int res = 0;
        for (; i > 0; i -= i & -i) res += bit[i];
        return res;
    }
};

template <int N, class It>
lli COUNT_INVS(It sta, It fin) {
    static BIT<N> bit;
    bit.init(N);

    lli res = 0;
    auto it = fin;

    do {
        it--;
//        cerr << "*it = " << *it << endl;
        res += bit.get(*it - 1);
        bit.add(*it, +1);
    }
    while (it != sta);

    return res;
}

const int MAX = 3e5 + 7;
int N;
string str;

void input() {
    cin >> N >> str;
    str = "#" + str;
}

lli COUNT_DIVISION_SWAPS() {
    int cnt[26], ptr[26];
    memset(cnt, 0, sizeof cnt);

    for (int i = 1; i <= 2 * N; i++)
        cnt[str[i] - 'a']++;

    static int tmp[MAX];

    memset(ptr, 0, sizeof ptr);
    for (int i = 1; i <= 2 * N; i++) {
        int c = str[i] - 'a';
        tmp[i] = (ptr[c] >= cnt[c] / 2) + 1;
        ptr[c]++;
    }

//    for (int i = 1; i <= 2 * N; i++)
//        cerr << tmp[i] << " ";
//    cerr << endl;

    string new_str = "#";
    for (int j : {1, 2})
        for (int i = 1; i <= 2 * N; i++) if (tmp[i] == j)
            new_str.push_back(str[i]);

    swap(str, new_str);

//    cerr << "str = " << str << endl;

    return COUNT_INVS<2>(tmp + 1, tmp + 2 * N + 1);
}

lli COUNT_REARRANGEMENT_SWAPS() {
    queue<int> inds[26];

    for (int i = 1; i <= N; i++) {
        inds[str[i] - 'a'].push(i);
//        cerr << str[i] << ": " << i << endl;
    }

    static int tmp[MAX];

    for (int i = 1; i <= N; i++) {
        int c = str[i + N] - 'a';
//        cerr << "c = " << c << " -> " << inds[c].front() << endl;
        tmp[i] = inds[c].front();
        inds[c].pop();
    }

    return COUNT_INVS<MAX>(tmp + 1, tmp + N + 1);
}

void solve() {
    lli ans = 0;
    ans += COUNT_DIVISION_SWAPS();
    ans += COUNT_REARRANGEMENT_SWAPS();
    cout << ans;
}

int main(void) {
    if (fopen(INPFILE, "r")) {
        freopen(INPFILE, "r", stdin);
        freopen(OUTFILE, "w", stdout);
    }
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    input();
    solve();

    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  119 |         freopen(INPFILE, "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
Main.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         freopen(OUTFILE, "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 4 ms 2128 KB Output is correct
3 Correct 6 ms 2856 KB Output is correct
4 Correct 9 ms 3432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 4 ms 2128 KB Output is correct
3 Correct 6 ms 2856 KB Output is correct
4 Correct 9 ms 3432 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1476 KB Output is correct
8 Correct 1 ms 1480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 4 ms 2128 KB Output is correct
3 Correct 6 ms 2856 KB Output is correct
4 Correct 9 ms 3432 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 10 ms 3416 KB Output is correct
7 Correct 10 ms 3416 KB Output is correct
8 Correct 10 ms 3424 KB Output is correct
9 Correct 10 ms 3468 KB Output is correct
10 Correct 10 ms 3464 KB Output is correct
11 Correct 9 ms 3444 KB Output is correct
12 Correct 9 ms 3292 KB Output is correct
13 Correct 9 ms 3308 KB Output is correct
14 Correct 9 ms 3308 KB Output is correct
15 Correct 9 ms 3336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1480 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1476 KB Output is correct
7 Correct 1 ms 1492 KB Output is correct
8 Correct 1 ms 1480 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
11 Correct 1 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 4 ms 2128 KB Output is correct
3 Correct 6 ms 2856 KB Output is correct
4 Correct 9 ms 3432 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1476 KB Output is correct
8 Correct 1 ms 1480 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 10 ms 3416 KB Output is correct
11 Correct 10 ms 3416 KB Output is correct
12 Correct 10 ms 3424 KB Output is correct
13 Correct 10 ms 3468 KB Output is correct
14 Correct 10 ms 3464 KB Output is correct
15 Correct 9 ms 3444 KB Output is correct
16 Correct 9 ms 3292 KB Output is correct
17 Correct 9 ms 3308 KB Output is correct
18 Correct 9 ms 3308 KB Output is correct
19 Correct 9 ms 3336 KB Output is correct
20 Correct 1 ms 1492 KB Output is correct
21 Correct 1 ms 1492 KB Output is correct
22 Correct 1 ms 1480 KB Output is correct
23 Correct 1 ms 1492 KB Output is correct
24 Correct 1 ms 1492 KB Output is correct
25 Correct 1 ms 1476 KB Output is correct
26 Correct 1 ms 1492 KB Output is correct
27 Correct 1 ms 1480 KB Output is correct
28 Correct 1 ms 1492 KB Output is correct
29 Correct 1 ms 1492 KB Output is correct
30 Correct 1 ms 1492 KB Output is correct
31 Correct 1 ms 1492 KB Output is correct
32 Correct 10 ms 3416 KB Output is correct
33 Correct 9 ms 3420 KB Output is correct
34 Correct 10 ms 3424 KB Output is correct
35 Correct 9 ms 3476 KB Output is correct
36 Correct 9 ms 3428 KB Output is correct
37 Correct 9 ms 3324 KB Output is correct
38 Correct 10 ms 3312 KB Output is correct
39 Correct 9 ms 3312 KB Output is correct
40 Correct 9 ms 3264 KB Output is correct
41 Correct 10 ms 3368 KB Output is correct