Submission #647137

#TimeUsernameProblemLanguageResultExecution timeMemory
647137AlmaEkoeko (COCI21_ekoeko)C++14
110 / 110
22 ms5852 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second using ll = long long; using ii = pair<int,int>; const int INF = 1e9; const ll LLINF = 1e18; using vi = vector<int>; using vvi = vector<vi>; void setIO (string fileName) { ios::sync_with_stdio(false); cin.tie(NULL); if (fileName != "std") { freopen((fileName + ".in").c_str(), "r", stdin); freopen((fileName + ".out").c_str(), "w", stdout); } } int n; vi bit; void update (int i, int val) { for (; i <= n; i += i&-i) { bit[i] += val; } } int query (int i) { int sum = 0; for (; i > 0; i -= i&-i) { sum += bit[i]; } return sum; } int main() { setIO("std"); cin >> n; n *= 2; string s; cin >> s; vi a(n), b, c; for (int i = 0; i < n; i++) { a[i] = s[i] - 'a'; } c = a; sort(c.begin(), c.end()); vi frec(26, 0); for (int i = 0; i < n; i += 2) { frec[c[i]]++; } for (int i = 0; i < n; i++) { if (frec[a[i]] > 0) { b.push_back(a[i]); frec[a[i]]--; } } vector<queue<int>> pos(26); for (int i = 0; i < n/2; i++) { b.push_back(b[i]); pos[b[i]].push(i); } for (int i = 0; i < n/2; i++) { b.push_back(b[i]); pos[b[i]].push(n/2 + i); } // turn a into b // obtain permutation -> count inversions vi perm(n); for (int i = 0; i < n; i++) { int idx = pos[a[i]].front(); pos[a[i]].pop(); perm[i] = idx+1; } // for (int i = 0; i < n; i++) { // cout << a[i] << ' '; // } cout << '\n'; // for (int i = 0; i < n; i++) { // cout << b[i] << ' '; // } cout << '\n'; // for (int i = 0; i < n; i++) { // cout << perm[i] << ' '; // } cout << '\n'; ll ans = 0; bit = vi(n+1, 0); for (int i = n-1; i >= 0; i--) { ans += query(perm[i]-1); update(perm[i], 1); } cout << ans << '\n'; return 0; /* aaaabbbb aabb aaababbb aabbaabb */ }

Compilation message (stderr)

Main.cpp: In function 'void setIO(std::string)':
Main.cpp:18:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |         freopen((fileName + ".in").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:19:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |         freopen((fileName + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...