답안 #1003787

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1003787 2024-06-20T17:49:30 Z vjudge1 Ekoeko (COCI21_ekoeko) C++17
110 / 110
181 ms 1384 KB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define mp make_pair
#define int long long

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<double> cd;

const int MAXN = 1e5+10;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);

int tree[MAXN];

void upd(int pos, int x, int lim){ for(;pos<=lim;pos+=(pos&-pos)) tree[pos]+=x; }
int query(int pos){
	int ret=0;
	for(;pos>0;pos-=(pos&-pos)) ret+=tree[pos];
	return ret;
}

void solve(){
	int n; cin >> n;
	string s; cin >> s;
	
	vector<int> freq(26, 0);
	for(auto u : s) freq[(int)(u-'a')]++;
	
	string s1="", s2="";
	int ans=0, tot=0;
	vector<int> occ(26, 0);
	for(auto u : s){
		occ[(int)(u-'a')]++;
		
		if(occ[(int)(u-'a')]>(freq[(int)(u-'a')]>>1)){
			tot++;
			s2+=u;
		}
		else{
			ans+=tot;
			s1+=u;
		}
	}
	
	//~ int lim=(int)s2.size();
	
	//~ vector<queue<int>> id(26);
	//~ for(int i=0;i<(int)s2.size();i++) id[(int)(s2[i]-'a')].push(i+1);
	
	//~ for(int i=1;i<=(int)s2.size();i++) upd(i, i, lim), upd(i+1, -i, lim);
	
	for(int i=0;i<(int)s1.size();i++){
		int ptr=i;
		while(s2[ptr]!=s1[i]) ptr++;
		for(int j=ptr-1;j>=i;j--){
			ans++;
			swap(s2[j], s2[j+1]);
		}
	}
	
	cout << ans << "\n";
}

int32_t main(){
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	
	int tt=1;
	//~ cin >> tt;
	while(tt--) solve();
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 1000 KB Output is correct
4 Correct 3 ms 1384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 1000 KB Output is correct
4 Correct 3 ms 1384 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 1000 KB Output is correct
4 Correct 3 ms 1384 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 136 ms 1028 KB Output is correct
7 Correct 127 ms 1000 KB Output is correct
8 Correct 96 ms 1000 KB Output is correct
9 Correct 102 ms 996 KB Output is correct
10 Correct 144 ms 996 KB Output is correct
11 Correct 163 ms 996 KB Output is correct
12 Correct 105 ms 1144 KB Output is correct
13 Correct 120 ms 1000 KB Output is correct
14 Correct 112 ms 1132 KB Output is correct
15 Correct 142 ms 1000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 600 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 1000 KB Output is correct
4 Correct 3 ms 1384 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 136 ms 1028 KB Output is correct
11 Correct 127 ms 1000 KB Output is correct
12 Correct 96 ms 1000 KB Output is correct
13 Correct 102 ms 996 KB Output is correct
14 Correct 144 ms 996 KB Output is correct
15 Correct 163 ms 996 KB Output is correct
16 Correct 105 ms 1144 KB Output is correct
17 Correct 120 ms 1000 KB Output is correct
18 Correct 112 ms 1132 KB Output is correct
19 Correct 142 ms 1000 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 600 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 181 ms 1000 KB Output is correct
33 Correct 174 ms 1172 KB Output is correct
34 Correct 150 ms 1000 KB Output is correct
35 Correct 154 ms 1000 KB Output is correct
36 Correct 149 ms 1168 KB Output is correct
37 Correct 132 ms 996 KB Output is correct
38 Correct 154 ms 996 KB Output is correct
39 Correct 174 ms 1296 KB Output is correct
40 Correct 165 ms 1128 KB Output is correct
41 Correct 154 ms 996 KB Output is correct