답안 #796072

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796072 2023-07-28T05:48:03 Z cig32 Ekoeko (COCI21_ekoeko) C++17
110 / 110
9 ms 3572 KB
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
  if(p==0) return 1 % MOD;
  int r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
int inv(int b) { 
  return bm(b, MOD-2);
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
int bit[MAXN];
void add(int x, int v) {
  for(;x<MAXN;x+=x&-x) bit[x] += v;
}
int sum(int x) {
  int s = 0;
  for(;x;x-=x&-x) s += bit[x];
  return s;
}
void solve(int tc) {
  int n;
  cin>>n;
  string s;
  cin>>s;
  int freq[2][26];
  for(int i=0;i<2;i++)for(int j=0;j<26;j++)freq[i][j]=0;
  for(int i=0;i<n;i++)freq[0][s[i]-'a']++;
  for(int i=n;i<2*n;i++)freq[1][s[i]-'a']++;
  int ans=0;
  int cnt[2]={0,0};
  bool move[2*n];
  for(int i=0;i<2*n;i++)move[i]=0;
  for(int i=n-1;i>=0;i--){
    if(freq[0][s[i]-'a']>freq[1][s[i]-'a']) {
      move[i]=1;
      freq[0][s[i]-'a']--;
      freq[1][s[i]-'a']++;
      cnt[0]++;
      ans+=n-cnt[0]-i;
    }
  }
  for(int i=n;i<2*n;i++){
    if(freq[1][s[i]-'a']>freq[0][s[i]-'a']) {
      move[i]=1;
      freq[1][s[i]-'a']--;
      freq[0][s[i]-'a']++;
      cnt[1]++;
      ans+=i-n-(cnt[1]-1);
    }
  }
  ans+=cnt[0]*cnt[1];
  string t;
  for(int i=0;i<n;i++) if(!move[i]) t+=s[i];
  for(int i=n;i<2*n;i++) if(move[i]) t+=s[i];
  for(int i=0;i<n;i++) if(move[i]) t+=s[i];
  for(int i=n;i<2*n;i++) if(!move[i]) t+=s[i];
  s=t;
  int a[n];
  queue<int>id[26];
  for(int i=0;i<n;i++){
    id[s[i]-'a'].push(i);
  }
  for(int i=0;i<n;i++) {
    a[i] = id[s[i+n]-'a'].front() + 1;
    id[s[i+n]-'a'].pop();
    ans += sum(n) - sum(a[i]);
    add(a[i], 1);
  }
  cout << ans << "\n";


}
int32_t main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
// 勿忘初衷
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 3 ms 1364 KB Output is correct
3 Correct 5 ms 2428 KB Output is correct
4 Correct 8 ms 3476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 3 ms 1364 KB Output is correct
3 Correct 5 ms 2428 KB Output is correct
4 Correct 8 ms 3476 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 3 ms 1364 KB Output is correct
3 Correct 5 ms 2428 KB Output is correct
4 Correct 8 ms 3476 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 7 ms 3572 KB Output is correct
7 Correct 7 ms 3560 KB Output is correct
8 Correct 8 ms 3444 KB Output is correct
9 Correct 8 ms 3444 KB Output is correct
10 Correct 8 ms 3396 KB Output is correct
11 Correct 7 ms 3412 KB Output is correct
12 Correct 8 ms 3332 KB Output is correct
13 Correct 7 ms 3328 KB Output is correct
14 Correct 7 ms 3316 KB Output is correct
15 Correct 7 ms 3188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 3 ms 1364 KB Output is correct
3 Correct 5 ms 2428 KB Output is correct
4 Correct 8 ms 3476 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 7 ms 3572 KB Output is correct
11 Correct 7 ms 3560 KB Output is correct
12 Correct 8 ms 3444 KB Output is correct
13 Correct 8 ms 3444 KB Output is correct
14 Correct 8 ms 3396 KB Output is correct
15 Correct 7 ms 3412 KB Output is correct
16 Correct 8 ms 3332 KB Output is correct
17 Correct 7 ms 3328 KB Output is correct
18 Correct 7 ms 3316 KB Output is correct
19 Correct 7 ms 3188 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 336 KB Output is correct
32 Correct 7 ms 3572 KB Output is correct
33 Correct 7 ms 3444 KB Output is correct
34 Correct 9 ms 3444 KB Output is correct
35 Correct 8 ms 3444 KB Output is correct
36 Correct 8 ms 3320 KB Output is correct
37 Correct 7 ms 3316 KB Output is correct
38 Correct 8 ms 3336 KB Output is correct
39 Correct 7 ms 3316 KB Output is correct
40 Correct 7 ms 3316 KB Output is correct
41 Correct 7 ms 3200 KB Output is correct