# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
872512 | 2023-11-13T08:55:09 Z | HossamHero7 | Ekoeko (COCI21_ekoeko) | C++14 | 4 ms | 2292 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<char> 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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 364 KB | Output is correct |
2 | Incorrect | 4 ms | 2292 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 364 KB | Output is correct |
2 | Incorrect | 4 ms | 2292 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 364 KB | Output is correct |
2 | Incorrect | 4 ms | 2292 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 344 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 | 1 ms | 348 KB | Output is correct |
6 | Correct | 1 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 | 348 KB | Output is correct |
10 | Correct | 1 ms | 348 KB | Output is correct |
11 | Correct | 0 ms | 348 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 364 KB | Output is correct |
2 | Incorrect | 4 ms | 2292 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |