이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ll long long
#ifndef local
#include "shoes.h"
#endif
using namespace std;
const int MAXN = 1e5+5;
int BIT[MAXN];
void add(int pos,int val) {
for (pos++;pos < MAXN;pos+=pos&-pos)
BIT[pos]+=val;
}
int get(int pos) {
int s = 0;
for (pos++;pos;pos-=pos&-pos)
s += BIT[pos];
return s;
}
ll count_swaps(vector<int> a) {
int n = a.size();
ll ans = 0;
vector< vector<vector<int>> > v(2,vector< vector<int> >(n+1));
for (int i = 0;i < n; i++) {
if (a[i] < 0) v[1][-a[i]].push_back(i);
else v[0][a[i]].push_back(i);
add(i,1);
}
vector<int> vis(n,1);
for (int i = 1; i <= n; i++)
for (int t = 0; t < 2; t++)
reverse(v[t][i].begin(),v[t][i].end());
for (int i = 0; i < n;i++) {
if (vis[i] == 0) continue;
int x = a[i];
int val = abs(x);
int t = (x < 0 ? 0 : 1);
int ps = v[t][val].back();
vis[i] = 0;
v[t][val].pop_back();
v[!t][val].pop_back();
if (i+1 < ps) ans+= get(ps-1) - get(i);
add(ps,-1);
add(i,-1);
if (t == 1) ans++;
vis[ps] = 0;
}
return ans;
}
#ifdef local
main() {
int n; cin >> n;
vector<int> v(n);
for (int i = 0;i < n; i++) cin >> v[i];
cout << count_swaps(v);
}
#endif
# | 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... |