Submission #1083600

# Submission time Handle Problem Language Result Execution time Memory
1083600 2024-09-03T14:46:01 Z RiverFlow Ekoeko (COCI21_ekoeko) C++14
110 / 110
9 ms 3688 KB
#include <bits/stdc++.h>

#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maxi(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
    if (a > b) { a = b; return 1; } return 0;
}

const int N = (int)2e5 + 9;
const int mod = (int)1e9 + 7;

void prepare(); void main_code();

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    const bool MULTITEST = 0; prepare();
    int num_test = 1; if (MULTITEST) cin >> num_test;
    while (num_test--) { main_code(); }
}

void prepare() {};

int dir[N], p[N];
vector<int> pos[26];

template<typename T>
struct fenwick {
    int n; vector<T> bit; bool up = 0;
    fenwick() {};
    fenwick(int _n, bool _up) { n = _n; bit.resize(n + 7); up = _up; }
    void upd(int id, T val) {
        if (up) for(; id <= n; id += id & -id) bit[id] += val;
        else    for(; id  > 0; id -= id & -id) bit[id] += val;
    }
    T get(int id) {
        T ans = 0;
        if (up) for(; id  > 0; id -= id & -id) ans += bit[id];
        else    for(; id <= n; id += id & -id) ans += bit[id];
        return ans;
    }
};


void main_code() {
    int n; cin >> n;
    string s; cin >> s;
    FOR(i, 0, sz(s) - 1) pos[s[i]-'a'].push_back(i);

    long long ans = 0;
    vec<int> cnt(2, 0);
    FOR(i, 0, 25) {
        int j = sz(pos[i]);
        for(int t = 0; t < j; ++t) {
            if (t < j / 2 and pos[i][t] >= n) {
                ++cnt[1]; // qua ben phai
                dir[pos[i][t]] = 2;
            }
            if (t >= j / 2 and pos[i][t] < n) {
                ++cnt[0];
                dir[pos[i][t]] = 1;
            }
        }
    }
    ans += 1LL * cnt[0] * cnt[1];
    vector<int> p1, p2;
    FOR(i, 0, sz(s) - 1) {
        if (dir[i] != 1 and (i < n or dir[i] == 2))
            p1.push_back(i);
        else
            p2.push_back(i);
    }
    int cx = 0;
    FOR(i, 0, sz(p1) - 1) {
        if (p1[i] >= n) {
            ans += abs(p1[i] - n - cx);
            ++cx;
        }
    }
    cx = 0;
    FOD(i, sz(p2) - 1, 0) {
        if (p2[i] < n) {
            ans += abs(p2[i] - (n - 1 - cx));
            ++cx;
        }
    }
    FOR(i, 0, 25) pos[i].clear();
    FOR(i, 0, sz(p1) - 1) {
        pos[s[p1[i]] - 'a'].push_back(i);
    }
    fenwick<int> bit(n, 0);
    FOR(i, 0, 25) reverse(all(pos[i]));
    FOR(i, 0, sz(p2) - 1) {
        p[i] = pos[s[p2[i]] - 'a'].back();
        pos[s[p2[i]] - 'a'].pop_back();
        ans += bit.get(p[i] + 1);
        bit.upd(p[i] + 1, 1);
    }
    cout << ans;
}


/*     Let the river flows naturally     */

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 2 ms 1688 KB Output is correct
3 Correct 5 ms 2788 KB Output is correct
4 Correct 8 ms 3688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 2 ms 1688 KB Output is correct
3 Correct 5 ms 2788 KB Output is correct
4 Correct 8 ms 3688 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 2 ms 1688 KB Output is correct
3 Correct 5 ms 2788 KB Output is correct
4 Correct 8 ms 3688 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 7 ms 3292 KB Output is correct
7 Correct 7 ms 3360 KB Output is correct
8 Correct 7 ms 3404 KB Output is correct
9 Correct 7 ms 3428 KB Output is correct
10 Correct 7 ms 3364 KB Output is correct
11 Correct 6 ms 3324 KB Output is correct
12 Correct 7 ms 3172 KB Output is correct
13 Correct 9 ms 3240 KB Output is correct
14 Correct 7 ms 3100 KB Output is correct
15 Correct 6 ms 3108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 2 ms 1688 KB Output is correct
3 Correct 5 ms 2788 KB Output is correct
4 Correct 8 ms 3688 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 7 ms 3292 KB Output is correct
11 Correct 7 ms 3360 KB Output is correct
12 Correct 7 ms 3404 KB Output is correct
13 Correct 7 ms 3428 KB Output is correct
14 Correct 7 ms 3364 KB Output is correct
15 Correct 6 ms 3324 KB Output is correct
16 Correct 7 ms 3172 KB Output is correct
17 Correct 9 ms 3240 KB Output is correct
18 Correct 7 ms 3100 KB Output is correct
19 Correct 6 ms 3108 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 1 ms 344 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 7 ms 3456 KB Output is correct
33 Correct 8 ms 3360 KB Output is correct
34 Correct 7 ms 3428 KB Output is correct
35 Correct 6 ms 3328 KB Output is correct
36 Correct 6 ms 3428 KB Output is correct
37 Correct 7 ms 3328 KB Output is correct
38 Correct 7 ms 3236 KB Output is correct
39 Correct 7 ms 3428 KB Output is correct
40 Correct 6 ms 3116 KB Output is correct
41 Correct 7 ms 3108 KB Output is correct