Submission #1322665

#TimeUsernameProblemLanguageResultExecution timeMemory
1322665wedonttalkanymoreEkoeko (COCI21_ekoeko)C++20
110 / 110
29 ms16404 KiB
#include <bits/stdc++.h>
/*
    Chang ki si xuyen man dem
    Di lac vao trong giac mong
*/
using namespace std;
using ll = long long;

#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second

const ll N = 5e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19;

int n;
string s;
int a[N];
vector <int> pos[30];
set <int> pos1[30];
int b[N];
vector <int> tmp, tmp1;
int c[N];

struct Fenwick {
    vector <int> bit;
    int sz;
    Fenwick(int _n) {
        bit.assign(_n + 5, 0);
        sz = _n;
    }
    void update(int i, int val) {
        while(i <= sz) {
            bit[i] += val;
            i += i & -i;
        }
    }
    int get(int i) {
        int res = 0;
        while(i > 0) {
            res += bit[i];
            i -= i & -i;
        }
        return res;
    }
} fw(N);

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    if (fopen(".inp", "r")) {
        freopen(".inp", "r", stdin);
        freopen(".out", "w", stdout);
    }
    cin >> n >> s;
    for (int i = 1; i <= 2 * n; i++) {
        a[i] = s[i - 1] - 'a';
        pos[a[i]].push_back(i);
    }
    for (int i = 0; i < 26; i++) {
        int have = pos[i].size() / 2;
        for (int j = 0; j < pos[i].size(); j++) {
            if (j < have) {
                b[pos[i][j]] = 1;
            }
            else {
                b[pos[i][j]] = 2;
            }
        }
    }
    int ans = 0;
    for (int i = 1, cnt = 0; i <= 2 * n; i++) {
        if (b[i] == 1) {
            ans += cnt;
            tmp.push_back(a[i]);
        }
        else {
            cnt++;
            tmp1.push_back(a[i]);
        }
    }
    for (int i = 0; i < tmp1.size(); i++) {
        pos1[tmp1[i]].insert(i + 1);
    }
    // cout << tmp.size() << ' ' << n << '\n';
    for (int i = 0; i < tmp.size(); i++) {
        int x = *pos1[tmp[i]].begin();
        pos1[tmp[i]].erase(pos1[tmp[i]].begin());
        c[i + 1] = x;
    }
    // for (int i = 1; i <= n; i++) cout << c[i] << ' ';
    vector <int> tt(n + 5, 0);
    for (int i = 1; i <= n; i++) tt[c[i]] = i;
    for (int i = 1; i <= n; i++) {
        ans += fw.get(n - tt[i] + 1);
        fw.update(n - tt[i] + 1, 1);
    }
    cout << ans << '\n';
    return 0;
}

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |         freopen(".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   53 |         freopen(".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...