Submission #1114343

#TimeUsernameProblemLanguageResultExecution timeMemory
1114343VinhLuuLine Town (CCO23_day1problem3)C++17
13 / 25
140 ms40840 KiB
#include <bits/stdc++.h> #define int long long #define ll long long #define all(lpv) lpv.begin(), lpv.end() #define fi first #define se second using namespace std; typedef pair<int,int> pii; const int N = 1e6 + 5; const int oo = 1e16; const int mod = 1e6 + 3; int n, a[N], m, zero; void add(int &x,int y){ if(x == -1) x = y; else x = min(x, y); } int bit[N]; void update(int i,int val){ for(; i <= n; i += i & -i) bit[i] += val; } int get(int i){ int ret = 0; for(; i; i -= i & -i) ret += bit[i]; return ret; } namespace Nlog{ tuple<int,int,int> save[N]; void solve(){ int ans = -1; deque<int> col[2], rev[2]; for(int i = 1; i <= n; i ++){ if(a[i] == 0){ continue; } int x = abs(a[i]); if(i & 1){ if(a[i] > 0) col[0].push_back(i); else col[1].push_back(i); }else{ if(a[i] > 0) col[1].push_back(i); else col[0].push_back(i); } if((n - i + 1) & 1){ if(a[i] > 0) rev[0].push_back(i); else rev[1].push_back(i); }else{ if(a[i] > 0) rev[1].push_back(i); else rev[0].push_back(i); } } int le = 0, ri = 0; int tmp = n - zero; int ret = 0; // for(auto j : rev[0]) cerr << j << " "; // cerr << "\n"; while(le < tmp){ int nxt = ((le + 1) & 1); if(col[nxt].empty()) break; le++; int x = col[nxt].front(); // cerr << nxt << " " << x << " r\n"; ret += x - 1 - get(x - 1); update(x, 1); // cerr << x << " clear\n"; if((n - x + 1) & 1){ if(a[x] > 0){ // cerr << "C0\n"; rev[0].pop_front(); } else{ // cerr << " C1\n"; rev[1].pop_front(); } }else{ if(a[x] > 0){ // cerr << "C1\n"; rev[1].pop_front(); } else{ // cerr << "C0\n"; rev[0].pop_front(); } } save[le] = {nxt, x, x - 1 - get(x - 1)}; col[nxt].pop_front(); } // cerr << le << " " << ret << " t\n"; while(ri < tmp - le){ int nxt = (((ri + 1) & 1) ^ 1); if(rev[nxt].empty()) break; ri++; int x = rev[nxt].back(); // cerr << nxt << " " << x << " f\n"; ret += n - x - (get(n) - get(x)); update(x, 1); rev[nxt].pop_back(); } // cerr << ri << " r\n"; // cerr << " satrt\n"; while(le >= 0){ if(le + ri == tmp){ add(ans, ret); } if(!le) break; int nxt, x, val; tie(nxt, x, val) = save[le]; col[nxt].push_front(x); update(x, -1); // cerr << x << " rescue\n"; ret -= x - 1 - get(x - 1); if((n - x + 1) & 1){ if(a[x] > 0) rev[0].push_front(x); else rev[1].push_front(x); }else{ if(a[x] > 0) rev[1].push_front(x); else rev[0].push_front(x); } le--; // cerr << le << " " << ret << " r\n"; while(ri < tmp - le){ int _right = (((ri + 1) & 1) ^ 1); if(rev[_right].empty()) break; int pos = rev[_right].back(); // cerr << _right << " " << pos << " e\n"; ri++; ret += (n - pos - (get(n) - get(pos))); update(pos, 1); rev[_right].pop_back(); } } cout << ans; } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define task "v" if(fopen(task ".inp","r")){ freopen(task ".inp","r",stdin); freopen(task ".out","w",stdout); } cin >> n; vector<int> rrh; for(int i = 1; i <= n; i ++){ cin >> a[i]; zero += (a[i] == 0); if(a[i] != 0) rrh.push_back(abs(a[i])); } sort(all(rrh)); rrh.resize(unique(all(rrh)) - rrh.begin()); m = (int)rrh.size(); for(int i = 1; i <= n; i ++){ if(a[i] == 0) continue; if(a[i] < 0) a[i] = -(lower_bound(all(rrh), abs(a[i])) - rrh.begin() + 1); else a[i] = lower_bound(all(rrh), abs(a[i])) - rrh.begin() + 1; } Nlog :: solve(); } /* 5 0 -1 -1 0 1 */

Compilation message (stderr)

Main.cpp: In function 'void Nlog::solve()':
Main.cpp:44:11: warning: unused variable 'x' [-Wunused-variable]
   44 |       int x = abs(a[i]);
      |           ^
Main.cpp: In function 'int main()':
Main.cpp:158:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |     freopen(task ".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:159:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  159 |     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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...