답안 #536439

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
536439 2022-03-13T10:15:07 Z inksamurai Ekoeko (COCI21_ekoeko) C++17
110 / 110
17 ms 4952 KB
#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3HspL4A ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vec(int);
void print(){cout<<"\n";}
template<class T,class...E>
void print(const T&v,const E&...u){cout<<v<<' ',print(u...);}
// e

struct fenwick{
	int n;
	vector <int> bit;
	fenwick(int m){
		init(m);
	}
	void init(int m){
		n=m;
		bit.resize(n,0);
	}
	int get(int i){
		int s=0;
		while(i){
			s+=bit[i];
			i-=i&-i;
		}
		return s;
	}
	void add(int i,int x){
		while(i<=n){
			bit[i]+=x;
			i+=i&-i;
		}
	}
};

signed main(){
_3HspL4A;
	int n;
	cin>>n;
	string s;
	cin>>s;
	const int m=2*n;
	vec(vi) ids(26);
	rep(i,m){
		ids[s[i]-'a'].pb(i);
	}
	vi cnt(26);
	rep(i,26){
		int now=0;
		for(auto j:ids[i]){
			if(j<=n-1) cnt[i]+=1;
			else cnt[i]-=1;
		}
	}
	vi ndl,ndr;
	rep(i,26){
		if(cnt[i]>0){
			assert(cnt[i]%2==0);
			while(ids[i].back()>=n) ids[i].pop_back();
			rep(j,cnt[i]/2){
				ndl.pb(ids[i][sz(ids[i])-j-1]);
			}
		}else if(cnt[i]<0){
			assert(abs(cnt[i])%2==0);
			reverse(ids[i].begin(),ids[i].end());
			while(ids[i].back()<n) ids[i].pop_back();
			reverse(ids[i].begin(),ids[i].end());
			rep(j,abs(cnt[i]/2)){
				ndr.pb(ids[i][j]);
			}
		}
	}
	sort(ndl.begin(),ndl.end());
	sort(ndr.begin(),ndr.end());
	int ans=0;
	string sl="",sr="",adl="",adr="";
	{
		vi usd(m,0);
		for(auto v:ndl) usd[v]=1;
		for(auto v:ndr) usd[v]=1;
		int r=n-1;
		rep(i,n){
			if(!usd[i]){
				sl+=s[i];
			}else{
				ans+=r-i;
				r-=1;
				adl+=s[i];
			}
		}
		int l=n;
		rng(i,n,m){
			if(!usd[i]){
				sr+=s[i];
			}else{
				ans+=i-l;
				l+=1;
				adr+=s[i];
			}
		}
		ans+=sz(adr)*sz(adr);
	}
	sl+=adr;
	sr=adl+sr;
	rep(i,26){
		ids[i].clear();
	}
	rep(i,n){
		ids[sr[i]-'a'].pb(i);
	}
	rep(i,26){
		reverse(ids[i].begin(),ids[i].end());
	}
	fenwick bit(n+11);
	rep(i,n){
		int ch=sl[i]-'a';
		assert(sz(ids[ch]));
		// if(sz(ids[ch])==0){
		// 	print("nanii");
		// 	return 0;
		// }
		int j=ids[ch].back();
		ans+=j+bit.get(j+1)-i;
		bit.add(1,1);
		bit.add(j+2,-1);
		ids[ch].pop_back();
	}
	print(ans);
//	
	return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:60:7: warning: unused variable 'now' [-Wunused-variable]
   60 |   int now=0;
      |       ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 6 ms 2000 KB Output is correct
3 Correct 12 ms 3544 KB Output is correct
4 Correct 17 ms 4952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 6 ms 2000 KB Output is correct
3 Correct 12 ms 3544 KB Output is correct
4 Correct 17 ms 4952 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 6 ms 2000 KB Output is correct
3 Correct 12 ms 3544 KB Output is correct
4 Correct 17 ms 4952 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 15 ms 4140 KB Output is correct
7 Correct 15 ms 4064 KB Output is correct
8 Correct 15 ms 4064 KB Output is correct
9 Correct 16 ms 4064 KB Output is correct
10 Correct 14 ms 4064 KB Output is correct
11 Correct 13 ms 4192 KB Output is correct
12 Correct 12 ms 3936 KB Output is correct
13 Correct 12 ms 3936 KB Output is correct
14 Correct 14 ms 3932 KB Output is correct
15 Correct 12 ms 3936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 328 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 320 KB Output is correct
9 Correct 1 ms 328 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 1 ms 212 KB Output is correct
2 Correct 6 ms 2000 KB Output is correct
3 Correct 12 ms 3544 KB Output is correct
4 Correct 17 ms 4952 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 15 ms 4140 KB Output is correct
11 Correct 15 ms 4064 KB Output is correct
12 Correct 15 ms 4064 KB Output is correct
13 Correct 16 ms 4064 KB Output is correct
14 Correct 14 ms 4064 KB Output is correct
15 Correct 13 ms 4192 KB Output is correct
16 Correct 12 ms 3936 KB Output is correct
17 Correct 12 ms 3936 KB Output is correct
18 Correct 14 ms 3932 KB Output is correct
19 Correct 12 ms 3936 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 328 KB Output is correct
24 Correct 1 ms 340 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 320 KB Output is correct
28 Correct 1 ms 328 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 324 KB Output is correct
32 Correct 14 ms 4292 KB Output is correct
33 Correct 14 ms 4320 KB Output is correct
34 Correct 13 ms 4328 KB Output is correct
35 Correct 13 ms 4320 KB Output is correct
36 Correct 15 ms 4184 KB Output is correct
37 Correct 15 ms 4192 KB Output is correct
38 Correct 16 ms 4072 KB Output is correct
39 Correct 14 ms 4188 KB Output is correct
40 Correct 12 ms 4192 KB Output is correct
41 Correct 13 ms 4192 KB Output is correct