Submission #1003838

# Submission time Handle Problem Language Result Execution time Memory
1003838 2024-06-20T18:44:27 Z vjudge1 Ekoeko (COCI21_ekoeko) C++17
110 / 110
42 ms 13524 KB
#include<bits/stdc++.h>
#define endl '\n'
#define int long long

using namespace std;

const int N = 200010;

vector <int> c[26];

struct Segtree{
	int tree[4*N];
	Segtree(){
		for(int i = 0;i < 4*N;i++) tree[i] = 0;
	}
	int join(int a, int b){
		return a+b;
	}
	void update(int node, int l, int r, int pos, int val){
		if(l == r){
			tree[node] += val;
			return;
		}
		int mid=  (l+r)/2;
		if(l <= pos and pos <= mid) update(2*node, l, mid, pos, val);
		else update(2*node+1, mid+1, r, pos, val);
		tree[node] = join(tree[2*node], tree[2*node+1]);
		return;
	}
	int query(int node, int l, int r, int tl, int tr){
		if(l > tr or tl > r) return 0;
		if(l <= tl and tr <= r) return tree[node];
		int mid = (tl+tr)/2;
		return join(query(2*node, l, r, tl , mid), query(2*node+1, l, r, mid+1, tr));
	}
}seg;

vector <int> srt;
int tam[26];

int32_t main(){
	int n;
	cin>> n;
	string s;
	cin >> s;
	vector <int> v1, v2;
	for(int i = 0;i < 2*n;i++){
		c[s[i]-'a'].push_back(i+1);
	}
	for(int i = 0;i < 26;i++){
		tam[i] = c[i].size();
		tam[i] /= 2;
		tam[i]--;
	}
	for(int i = 2*n;i > 0;i--){
		if(tam[s[i-1]-'a'] < 0) continue;
		c[s[i-1]-'a'].pop_back();
		v2.push_back(i);
		v1.push_back(c[s[i-1]-'a'][tam[s[i-1]-'a']]);
		tam[s[i-1]-'a']--;
	}
	reverse(v1.begin(), v1.end());
	reverse(v2.begin(), v2.end());
	for(auto x : v1) srt.push_back(x);
	for(auto x : v2) srt.push_back(x);
	int res = 0;
	for(auto x : srt){
		//cout << x << ' ';
		res += seg.query(1,x, 2*n, 1, 2*n);
		seg.update(1, 1, 2*n, x, 1);
	}
	cout << res << endl;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 11 ms 9616 KB Output is correct
3 Correct 22 ms 11148 KB Output is correct
4 Correct 31 ms 12428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 11 ms 9616 KB Output is correct
3 Correct 22 ms 11148 KB Output is correct
4 Correct 31 ms 12428 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6696 KB Output is correct
7 Correct 3 ms 6492 KB Output is correct
8 Correct 2 ms 6700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 11 ms 9616 KB Output is correct
3 Correct 22 ms 11148 KB Output is correct
4 Correct 31 ms 12428 KB Output is correct
5 Correct 3 ms 6708 KB Output is correct
6 Correct 42 ms 13524 KB Output is correct
7 Correct 38 ms 12500 KB Output is correct
8 Correct 37 ms 13264 KB Output is correct
9 Correct 39 ms 13332 KB Output is correct
10 Correct 38 ms 12436 KB Output is correct
11 Correct 35 ms 13268 KB Output is correct
12 Correct 35 ms 13336 KB Output is correct
13 Correct 35 ms 13336 KB Output is correct
14 Correct 35 ms 13484 KB Output is correct
15 Correct 33 ms 12436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 6488 KB Output is correct
2 Correct 2 ms 6748 KB Output is correct
3 Correct 2 ms 6748 KB Output is correct
4 Correct 3 ms 6544 KB Output is correct
5 Correct 4 ms 6748 KB Output is correct
6 Correct 3 ms 6748 KB Output is correct
7 Correct 3 ms 6744 KB Output is correct
8 Correct 3 ms 6608 KB Output is correct
9 Correct 3 ms 6748 KB Output is correct
10 Correct 3 ms 6748 KB Output is correct
11 Correct 3 ms 6748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6488 KB Output is correct
2 Correct 11 ms 9616 KB Output is correct
3 Correct 22 ms 11148 KB Output is correct
4 Correct 31 ms 12428 KB Output is correct
5 Correct 2 ms 6492 KB Output is correct
6 Correct 2 ms 6696 KB Output is correct
7 Correct 3 ms 6492 KB Output is correct
8 Correct 2 ms 6700 KB Output is correct
9 Correct 3 ms 6708 KB Output is correct
10 Correct 42 ms 13524 KB Output is correct
11 Correct 38 ms 12500 KB Output is correct
12 Correct 37 ms 13264 KB Output is correct
13 Correct 39 ms 13332 KB Output is correct
14 Correct 38 ms 12436 KB Output is correct
15 Correct 35 ms 13268 KB Output is correct
16 Correct 35 ms 13336 KB Output is correct
17 Correct 35 ms 13336 KB Output is correct
18 Correct 35 ms 13484 KB Output is correct
19 Correct 33 ms 12436 KB Output is correct
20 Correct 2 ms 6488 KB Output is correct
21 Correct 2 ms 6748 KB Output is correct
22 Correct 2 ms 6748 KB Output is correct
23 Correct 3 ms 6544 KB Output is correct
24 Correct 4 ms 6748 KB Output is correct
25 Correct 3 ms 6748 KB Output is correct
26 Correct 3 ms 6744 KB Output is correct
27 Correct 3 ms 6608 KB Output is correct
28 Correct 3 ms 6748 KB Output is correct
29 Correct 3 ms 6748 KB Output is correct
30 Correct 3 ms 6748 KB Output is correct
31 Correct 2 ms 6492 KB Output is correct
32 Correct 38 ms 13268 KB Output is correct
33 Correct 39 ms 13268 KB Output is correct
34 Correct 37 ms 12440 KB Output is correct
35 Correct 34 ms 12696 KB Output is correct
36 Correct 37 ms 12436 KB Output is correct
37 Correct 36 ms 13172 KB Output is correct
38 Correct 35 ms 12200 KB Output is correct
39 Correct 32 ms 13268 KB Output is correct
40 Correct 32 ms 13336 KB Output is correct
41 Correct 36 ms 12436 KB Output is correct