Submission #540814

#TimeUsernameProblemLanguageResultExecution timeMemory
540814AlperenTEkoeko (COCI21_ekoeko)C++17
110 / 110
14 ms3652 KiB
#include <bits/stdc++.h> using namespace std; const int N = (1e5 + 5) * 2; int n, cnt[26]; long long ans; string str; vector<char> strv; vector<int> indx[26], v; struct Fenwick{ int tree[N]; int getsum(int r){ int sum = 0; for(int i = r; i > 0; i -= i & -i) sum += tree[i]; return sum; } int query(int l, int r){ if(l > r) return 0; return getsum(r) - getsum(l - 1); } void update(int pos, int val){ for(int i = pos; i < N; i += i & -i) tree[i] += val; } }; Fenwick fw; int main(){ ios_base::sync_with_stdio(false);cin.tie(NULL); cin >> n; cin >> str; n *= 2; for(auto c : str) cnt[c - 'a']++; for(int i = 0; i < n; i++){ if(cnt[str[i] - 'a'] > 0){ strv.push_back(str[i]); cnt[str[i] - 'a'] -= 2; } } for(int i = 0, m = strv.size(); i < m; i++) strv.push_back(strv[i]); for(int i = 0; i < n; i++) indx[strv[i] - 'a'].push_back(i + 1); for(int i = 0; i < 26; i++) reverse(indx[i].begin(), indx[i].end()); for(int i = 0; i < n; i++){ v.push_back(indx[str[i] - 'a'].back()); indx[str[i] - 'a'].pop_back(); } for(auto i : v){ ans += fw.query(i + 1, n); fw.update(i, 1); } cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...