답안 #922352

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
922352 2024-02-05T12:29:17 Z Shayan86 Ekoeko (COCI21_ekoeko) C++14
110 / 110
18 ms 5668 KB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define Mp          make_pair
#define sep         ' '
#define endl        '\n'
#define F	        first
#define S	        second
#define pb          push_back
#define all(x)      (x).begin(),(x).end()
#define kill(res)	cout << res << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define fast_io     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io     freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);

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

const ll A = 28;
const ll N = 2e5 + 50;
const ll Mod = 1e9 + 7;

ll n, num[N], ps[N];

string s, t1, t2;
vector<int> idx[A];

int bit[N];

void upd(int x, int y){
    for(; x <= n; x += x & (-x)) bit[x] += y;
}

int get(int x){
    ll out = 0;
    for(; x; x &= x-1) out += bit[x];
    return out;
}

int main(){
    fast_io;

    cin >> n >> s;
    s = "$" + s;

    for(int i = 0; i < A; i++){
        int cnt = 0, c = 0;
        for(int j = 1; j <= 2*n; j++) if(s[j] - 'a' == i) cnt++;
        for(int j = 1; j <= 2*n; j++) if(s[j] - 'a' == i) num[j] = ((++c) <= cnt/2 ? 1 : 2);
    }

    ll sum = 0;
    for(int i = 1; i <= 2*n; i++) ps[i] = ps[i-1] + num[i] - 1;
    for(int i = 1; i <= 2*n; i++) sum += ps[i] * (num[i] == 1);

    t1 = t2 = "$";
    for(int i = 1; i <= 2*n; i++){
        if(num[i] == 1) t1 += s[i];
        else t2 += s[i];
    }

    for(int i = 0; i < A; i++){
        for(int j = n; j > 0; j--) if(t2[j] - 'a' == i) idx[i].pb(j);
    }

    for(int i = 1; i <= n; i++){
        upd(idx[t1[i] - 'a'].back(), 1); sum += idx[t1[i] - 'a'].back() - get(idx[t1[i] - 'a'].back());
        idx[t1[i] - 'a'].pop_back();
    }

    cout << sum;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 5 ms 3636 KB Output is correct
3 Correct 8 ms 4708 KB Output is correct
4 Correct 14 ms 5668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 5 ms 3636 KB Output is correct
3 Correct 8 ms 4708 KB Output is correct
4 Correct 14 ms 5668 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2488 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 5 ms 3636 KB Output is correct
3 Correct 8 ms 4708 KB Output is correct
4 Correct 14 ms 5668 KB Output is correct
5 Correct 1 ms 2520 KB Output is correct
6 Correct 15 ms 5284 KB Output is correct
7 Correct 15 ms 5156 KB Output is correct
8 Correct 14 ms 5308 KB Output is correct
9 Correct 14 ms 5160 KB Output is correct
10 Correct 18 ms 5156 KB Output is correct
11 Correct 14 ms 5164 KB Output is correct
12 Correct 14 ms 5168 KB Output is correct
13 Correct 14 ms 5168 KB Output is correct
14 Correct 13 ms 4916 KB Output is correct
15 Correct 13 ms 4916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 5 ms 3636 KB Output is correct
3 Correct 8 ms 4708 KB Output is correct
4 Correct 14 ms 5668 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 2488 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2520 KB Output is correct
10 Correct 15 ms 5284 KB Output is correct
11 Correct 15 ms 5156 KB Output is correct
12 Correct 14 ms 5308 KB Output is correct
13 Correct 14 ms 5160 KB Output is correct
14 Correct 18 ms 5156 KB Output is correct
15 Correct 14 ms 5164 KB Output is correct
16 Correct 14 ms 5168 KB Output is correct
17 Correct 14 ms 5168 KB Output is correct
18 Correct 13 ms 4916 KB Output is correct
19 Correct 13 ms 4916 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 1 ms 2396 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 1 ms 2396 KB Output is correct
31 Correct 1 ms 2396 KB Output is correct
32 Correct 14 ms 5408 KB Output is correct
33 Correct 15 ms 5156 KB Output is correct
34 Correct 14 ms 5160 KB Output is correct
35 Correct 14 ms 5332 KB Output is correct
36 Correct 14 ms 5164 KB Output is correct
37 Correct 14 ms 5164 KB Output is correct
38 Correct 14 ms 5168 KB Output is correct
39 Correct 14 ms 5168 KB Output is correct
40 Correct 13 ms 4920 KB Output is correct
41 Correct 14 ms 4916 KB Output is correct