Submission #872514

# Submission time Handle Problem Language Result Execution time Memory
872514 2023-11-13T09:00:36 Z HossamHero7 Ekoeko (COCI21_ekoeko) C++14
110 / 110
12 ms 5000 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
struct BIT{
	vector<int> bit;	
	BIT(int n){
		bit.resize(n+1);
	}
	BIT(vector<int> &v){
		bit = v;
		for(int i=1;i<v.size();i++){
			bit[i+(i&(-i))] += bit[i];
		}
	}
	ll prefix(int i){
		ll ret = 0;
		while(i>0){
			ret += bit[i];
			i -= i&(-i);	
		}
		return ret;
	}
	ll sumQ(int l,int r){
		return prefix(r)-prefix(l-1);
	}
	void update(int x,int val){
		while(x<bit.size()){
			bit[x] += val;
			x += x&(-x);
		}
	}
};
ll minSwaps(string s1,string s2){
	int n = s1.size();
    BIT tree1(n+5);
	vector<vector<int>> idx1(27);
	vector<vector<int>> idx2(27);
	for(int i=0;i<n;i++){
		idx1[s1[i]-'a'].push_back(i);
		idx2[s2[i]-'a'].push_back(i);
	}
	vector<int> a(n);
	for(int i=0;i<26;i++){
		for(int j=0;j<idx1[i].size();j++){
			a[idx1[i][j]] = idx2[i][j];
		}
	}
	ll ans = 0;
	for(int i=0;i<n;i++){
		ans += tree1.sumQ(a[i]+2,n+1);
		tree1.update(a[i]+1,1);
	}
    return ans;
}
void solve(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    vector<int> frq(26);
    for(int i=0;i<2*n;i++) frq[s[i]-'a'] ++;
    vector<int> cnt(26);
    string tar = "";
    for(int i=0;i<2*n;i++){
        if(cnt[s[i]-'a'] < frq[s[i]-'a']/2) tar += s[i];
        cnt[s[i]-'a'] ++;
    }
    tar += tar;
    cout<<minSwaps(s,tar)<<endl;
}
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);		 cout.tie(0);
	int t=1;
	//cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

Compilation message

Main.cpp: In constructor 'BIT::BIT(std::vector<int>&)':
Main.cpp:12:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |   for(int i=1;i<v.size();i++){
      |               ~^~~~~~~~~
Main.cpp: In member function 'void BIT::update(int, int)':
Main.cpp:28:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |   while(x<bit.size()){
      |         ~^~~~~~~~~~~
Main.cpp: In function 'll minSwaps(std::string, std::string)':
Main.cpp:45:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |   for(int j=0;j<idx1[i].size();j++){
      |               ~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 3 ms 1940 KB Output is correct
3 Correct 7 ms 3372 KB Output is correct
4 Correct 11 ms 4484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 3 ms 1940 KB Output is correct
3 Correct 7 ms 3372 KB Output is correct
4 Correct 11 ms 4484 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 1 ms 456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 3 ms 1940 KB Output is correct
3 Correct 7 ms 3372 KB Output is correct
4 Correct 11 ms 4484 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 11 ms 4852 KB Output is correct
7 Correct 10 ms 4752 KB Output is correct
8 Correct 12 ms 4648 KB Output is correct
9 Correct 11 ms 4752 KB Output is correct
10 Correct 12 ms 5000 KB Output is correct
11 Correct 11 ms 4752 KB Output is correct
12 Correct 10 ms 4500 KB Output is correct
13 Correct 10 ms 4500 KB Output is correct
14 Correct 10 ms 4500 KB Output is correct
15 Correct 10 ms 4612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 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 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 3 ms 1940 KB Output is correct
3 Correct 7 ms 3372 KB Output is correct
4 Correct 11 ms 4484 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 1 ms 456 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 11 ms 4852 KB Output is correct
11 Correct 10 ms 4752 KB Output is correct
12 Correct 12 ms 4648 KB Output is correct
13 Correct 11 ms 4752 KB Output is correct
14 Correct 12 ms 5000 KB Output is correct
15 Correct 11 ms 4752 KB Output is correct
16 Correct 10 ms 4500 KB Output is correct
17 Correct 10 ms 4500 KB Output is correct
18 Correct 10 ms 4500 KB Output is correct
19 Correct 10 ms 4612 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 592 KB Output is correct
32 Correct 11 ms 4904 KB Output is correct
33 Correct 10 ms 4752 KB Output is correct
34 Correct 12 ms 4752 KB Output is correct
35 Correct 11 ms 4752 KB Output is correct
36 Correct 12 ms 4740 KB Output is correct
37 Correct 11 ms 4816 KB Output is correct
38 Correct 12 ms 4616 KB Output is correct
39 Correct 10 ms 4500 KB Output is correct
40 Correct 12 ms 4500 KB Output is correct
41 Correct 10 ms 4504 KB Output is correct