답안 #796064

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796064 2023-07-28T05:42:01 Z cig32 Ekoeko (COCI21_ekoeko) C++17
0 / 110
3 ms 1364 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-1-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;
    }
  }
  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 1 ms 340 KB Output is correct
2 Incorrect 3 ms 1364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 3 ms 1364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 3 ms 1364 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 1 ms 340 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 3 ms 1364 KB Output isn't correct
3 Halted 0 ms 0 KB -