답안 #850473

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
850473 2023-09-16T15:29:19 Z serifefedartar Ekoeko (COCI21_ekoeko) C++17
10 / 110
15 ms 4888 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());
    for (auto u : rightV)
        leftV.push_back(u);

    ll ans = 0;
    for (int i = 0; i < 2*n; i++) {
        ans += i - query(leftV[i]);
        update(leftV[i]);
    }
    cout << ans << "\n"; 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 4 ms 1876 KB Output is correct
3 Correct 7 ms 2912 KB Output is correct
4 Correct 10 ms 4384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 4 ms 1876 KB Output is correct
3 Correct 7 ms 2912 KB Output is correct
4 Correct 10 ms 4384 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Incorrect 1 ms 344 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 4 ms 1876 KB Output is correct
3 Correct 7 ms 2912 KB Output is correct
4 Correct 10 ms 4384 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Incorrect 15 ms 4888 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 4 ms 1876 KB Output is correct
3 Correct 7 ms 2912 KB Output is correct
4 Correct 10 ms 4384 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Incorrect 1 ms 344 KB Output isn't correct
7 Halted 0 ms 0 KB -