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 "shoes.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define X first
#define Y second
const int maxn = 2e5 + 5;
int n, m;
queue<int> pos[maxn][2];
pii a[maxn];
int pick[maxn];
int cost(pii x) {
if(x.first < x.second) return x.first + x.second - 1;
return x.first + x.second;
}
long long count_swaps(std::vector<int> s) {
n = s.size(); m = 0;
for(int i = 0; i < n; i++) {
int x = abs(s[i]);
pos[x][s[i] < 0].push(i);
// printf("%d: %d\n",x,s[i]<0);
if(!pos[x][0].empty() && !pos[x][1].empty()) {
a[++m] = {pos[x][1].front(), pos[x][0].front()};
// printf("{%d, %d}\n",a[m].first,a[m].second);
pos[x][0].pop(); pos[x][1].pop();
}
}
ll ans = 0;
for(int i = 1; i <= m; i++) pick[i] = 0;
for(int cur = 1; cur <= m; cur++) {
int best = 0;
for(int i = 1; i <= m; i++) {
if(pick[i]) continue;
if(!best || cost(a[i]) < cost(a[best])) best = i;
}
// printf("pick %d %d\n",a[best].first,a[best].second);
ans += cost(a[best]);
pick[best] = 1;
for(int i = 1; i <= m; i++) {
if(pick[i]) continue;
a[i].first += (a[best].first > a[i].first) + (a[best].second > a[i].first) - 2;
a[i].second += (a[best].first > a[i].second) + (a[best].second > a[i].second) - 2;
}
}
return ans;
}
# | 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... |