답안 #496191

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
496191 2021-12-21T01:02:09 Z model_code Ekoeko (COCI21_ekoeko) C++17
110 / 110
159 ms 3272 KB
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

const int MAXN = 100005;

int n;
long long sol=0;
char str[2*MAXN];
char s1[MAXN], s2[MAXN];

void make_anagram(){
    int total[26], cnt[26]; 
    fill(cnt, cnt+26, 0);
    fill(total, total+26, 0);
    for(int i=0;i<2*n;i++) total[str[i]-'a']++;
    int s1i=0, s2i=0;
    for(int i=0;i<n;i++){
        int c = str[i]-'a';
        if(cnt[c] >= total[c]/2){
            sol+=(n+s2i)-i;
            s2[s2i++] = str[i];
        }else{
            s1[s1i++] = str[i];
        }
        cnt[c]++;
    }
    int m=n;
    for(int i=n;i<2*n;i++){
        int c = str[i]-'a';
        if(cnt[c] < total[c]/2){
            sol+=i-m;
            m++;
            s1[s1i++] = str[i];
        }else{
            s2[s2i++] = str[i];
        }
        cnt[c]++;
    }
}

long long fen[2][MAXN];

void update(long long * fen, int idx, int val){
    for(idx++;idx<MAXN;idx+=idx&(-idx)){
        fen[idx] += val;
    }
}
long long query(long long * fen, int idx){
    long long res = 0;
    for(idx++;idx>0;idx-=idx&(-idx)){
        res += fen[idx];
    }
    return res;
}

void setup(long long * fen){
    for(int i=1;i<n;i++)
        update(fen, i, 1);
}
void update_after(long long * fen, int idx){
    update(fen, idx+1, -1);
}
int updated_idx(long long * fen, int idx){
    return query(fen, idx);
}

void rearrange(){
    vector<int> idx1[26], idx2[26];
    for(int i=n-1;i>=0;i--){
        idx1[s1[i]-'a'].push_back(i);
        idx2[s2[i]-'a'].push_back(i);
    }
    setup(fen[0]);
    setup(fen[1]);
    auto cost = [&] (int c) -> int {return idx1[c].size()?updated_idx(fen[0], idx1[c].back()) + updated_idx(fen[1], idx2[c].back()):10*MAXN;};
    for(int i=0;i<n;i++){
        int c = 0;
        for(int j=1;j<26;j++){
            if(cost(j) < cost(c)){
                c = j;
            }
        }
        sol += cost(c);
        update_after(fen[0], idx1[c].back());
        update_after(fen[1], idx2[c].back());
        idx1[c].pop_back();
        idx2[c].pop_back();
    }
}

int main(){
    scanf("%d", &n);
    scanf("%s", str);
    make_anagram();
    rearrange();
    printf("%lld\n", sol);
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:95:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
Main.cpp:96:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |     scanf("%s", str);
      |     ~~~~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 18 ms 1484 KB Output is correct
3 Correct 32 ms 2420 KB Output is correct
4 Correct 51 ms 3272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 18 ms 1484 KB Output is correct
3 Correct 32 ms 2420 KB Output is correct
4 Correct 51 ms 3272 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 18 ms 1484 KB Output is correct
3 Correct 32 ms 2420 KB Output is correct
4 Correct 51 ms 3272 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 159 ms 3208 KB Output is correct
7 Correct 157 ms 3196 KB Output is correct
8 Correct 142 ms 3088 KB Output is correct
9 Correct 143 ms 3180 KB Output is correct
10 Correct 154 ms 3136 KB Output is correct
11 Correct 136 ms 3140 KB Output is correct
12 Correct 147 ms 3136 KB Output is correct
13 Correct 142 ms 3120 KB Output is correct
14 Correct 157 ms 3084 KB Output is correct
15 Correct 139 ms 3048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 344 KB Output is correct
5 Correct 2 ms 344 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 392 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 18 ms 1484 KB Output is correct
3 Correct 32 ms 2420 KB Output is correct
4 Correct 51 ms 3272 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 159 ms 3208 KB Output is correct
11 Correct 157 ms 3196 KB Output is correct
12 Correct 142 ms 3088 KB Output is correct
13 Correct 143 ms 3180 KB Output is correct
14 Correct 154 ms 3136 KB Output is correct
15 Correct 136 ms 3140 KB Output is correct
16 Correct 147 ms 3136 KB Output is correct
17 Correct 142 ms 3120 KB Output is correct
18 Correct 157 ms 3084 KB Output is correct
19 Correct 139 ms 3048 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 2 ms 344 KB Output is correct
24 Correct 2 ms 344 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 392 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 0 ms 332 KB Output is correct
32 Correct 159 ms 3176 KB Output is correct
33 Correct 142 ms 3176 KB Output is correct
34 Correct 138 ms 3148 KB Output is correct
35 Correct 138 ms 3148 KB Output is correct
36 Correct 136 ms 3028 KB Output is correct
37 Correct 134 ms 3144 KB Output is correct
38 Correct 136 ms 3092 KB Output is correct
39 Correct 132 ms 3032 KB Output is correct
40 Correct 140 ms 3060 KB Output is correct
41 Correct 129 ms 3072 KB Output is correct