Submission #1083600

#TimeUsernameProblemLanguageResultExecution timeMemory
1083600RiverFlowEkoeko (COCI21_ekoeko)C++14
110 / 110
9 ms3688 KiB
#include <bits/stdc++.h> #define nl "\n" #define no "NO" #define yes "YES" #define fi first #define se second #define vec vector #define task "main" #define _mp make_pair #define ii pair<int, int> #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define evoid(val) return void(std::cout << val) #define FOR(i, a, b) for(int i = (a); i <= (b); ++i) #define FOD(i, b, a) for(int i = (b); i >= (a); --i) #define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin()) using namespace std; template<typename U, typename V> bool maxi(U &a, V b) { if (a < b) { a = b; return 1; } return 0; } template<typename U, typename V> bool mini(U &a, V b) { if (a > b) { a = b; return 1; } return 0; } const int N = (int)2e5 + 9; const int mod = (int)1e9 + 7; void prepare(); void main_code(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } const bool MULTITEST = 0; prepare(); int num_test = 1; if (MULTITEST) cin >> num_test; while (num_test--) { main_code(); } } void prepare() {}; int dir[N], p[N]; vector<int> pos[26]; template<typename T> struct fenwick { int n; vector<T> bit; bool up = 0; fenwick() {}; fenwick(int _n, bool _up) { n = _n; bit.resize(n + 7); up = _up; } void upd(int id, T val) { if (up) for(; id <= n; id += id & -id) bit[id] += val; else for(; id > 0; id -= id & -id) bit[id] += val; } T get(int id) { T ans = 0; if (up) for(; id > 0; id -= id & -id) ans += bit[id]; else for(; id <= n; id += id & -id) ans += bit[id]; return ans; } }; void main_code() { int n; cin >> n; string s; cin >> s; FOR(i, 0, sz(s) - 1) pos[s[i]-'a'].push_back(i); long long ans = 0; vec<int> cnt(2, 0); FOR(i, 0, 25) { int j = sz(pos[i]); for(int t = 0; t < j; ++t) { if (t < j / 2 and pos[i][t] >= n) { ++cnt[1]; // qua ben phai dir[pos[i][t]] = 2; } if (t >= j / 2 and pos[i][t] < n) { ++cnt[0]; dir[pos[i][t]] = 1; } } } ans += 1LL * cnt[0] * cnt[1]; vector<int> p1, p2; FOR(i, 0, sz(s) - 1) { if (dir[i] != 1 and (i < n or dir[i] == 2)) p1.push_back(i); else p2.push_back(i); } int cx = 0; FOR(i, 0, sz(p1) - 1) { if (p1[i] >= n) { ans += abs(p1[i] - n - cx); ++cx; } } cx = 0; FOD(i, sz(p2) - 1, 0) { if (p2[i] < n) { ans += abs(p2[i] - (n - 1 - cx)); ++cx; } } FOR(i, 0, 25) pos[i].clear(); FOR(i, 0, sz(p1) - 1) { pos[s[p1[i]] - 'a'].push_back(i); } fenwick<int> bit(n, 0); FOR(i, 0, 25) reverse(all(pos[i])); FOR(i, 0, sz(p2) - 1) { p[i] = pos[s[p2[i]] - 'a'].back(); pos[s[p2[i]] - 'a'].pop_back(); ans += bit.get(p[i] + 1); bit.upd(p[i] + 1, 1); } cout << ans; } /* Let the river flows naturally */

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen(task".out", "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...