# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1113371 | VinhLuu | Line Town (CCO23_day1problem3) | C++17 | 2076 ms | 65760 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 brute{
vector<int> even[N], odd[N];
void solve(){
int ans = -1;
for(int k = 0; k <= n - zero; k ++){
for(int i = 0; i <= m; i ++){
even[i].clear();
odd[i].clear();
}
for(int i = n; i >= 1; i --){
if(a[i] == 0){
even[0].push_back(i);
continue;
}
int x = abs(a[i]);
if(i & 1){
if(a[i] > 0) even[x].push_back(i);
else odd[x].push_back(i);
}else{
if(a[i] > 0) odd[x].push_back(i);
else even[x].push_back(i);
}
}
// cerr << k << " start\n";
bool ff = true;
int cnt = 0;
for(int i = 1; i <= n; i ++) bit[i] = 0;
for(int i = 1; i <= k; i ++){
int x = 1;
if(i & 1){
if(odd[x].empty()){
ff = false;
break;
}
int pos = odd[x].back();
odd[x].pop_back();
cnt += pos - 1 - get(pos - 1);
update(pos, 1);
}else{
if(even[x].empty()){
ff = false;
break;
}
int pos = even[x].back();
even[x].pop_back();
cnt += pos - 1 - get(pos - 1);
update(pos, 1);
}
}
// cerr << ff << " " << cnt << " g\n";
if(!ff) continue;
while(!even[0].empty()){
int pos = even[0].back();
even[0].pop_back();
cnt += pos - 1 - get(pos - 1);
update(pos, 1);
}
// cerr << k << " " << cnt << " y\n";
for(int i = k + zero + 1; i <= n; i ++){
int x = 1;
if(i & 1){
if(even[x].empty()){
ff = false;
break;
}
int pos = even[x].back();
even[x].pop_back();
cnt += pos - 1 - get(pos - 1);
update(pos, 1);
}else{
if(odd[x].empty()){
ff = false;
break;
}
int pos = odd[x].back();
odd[x].pop_back();
cnt += pos - 1 - get(pos - 1);
update(pos, 1);
}
}
if(!ff) continue;
// cerr << k << " " << cnt << " result\n";
add(ans, cnt);
}
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;
}
brute :: solve();
}
/*
5
0 -1 -1 0 1
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |