이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define INF 1e9+7
#define all(x) x.begin(),x.end()
using namespace std;
using namespace __gnu_pbds;
using ll=long long;
using pii=pair<int,int>;
using ppi=pair<int,pii>;
using oset=tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>;
template<typename T>
void _print(vector<T> x) {cerr<<"{"; for(auto e:x) cerr<<e<<","; cerr<<"}";}
void _print(pii x) {cerr<<"{"<<x.first<<","<<x.second<<"}";}
template<typename T>
void _print(T x) {cerr<<x;}
void dbg() {cerr<<endl;}
template<typename Head,typename... Tail>
void dbg(Head H,Tail... T) {
_print(H);
if(sizeof...(T)) cerr<<",";
else cerr<<"\"]";
dbg(T...);
}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:[\"",dbg(__VA_ARGS__)
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mxn=2e5+10;
map<int,queue<int>> q;
int t[mxn];
void add(int pos,int val) {
while(pos<mxn) {
t[pos]+=val;
pos+=pos&-pos;
}
}
int get(int pos) {
int res=0;
while(pos>0) {
res+=t[pos];
pos-=pos&-pos;
}
return res;
}
long long count_swaps(std::vector<int> s) {
int n=s.size();
ll ans=0;
for(int i=0;i<n;i++) {
q[s[i]].push(i);
}
for(int i=0;i<n;i++) {
if(q[s[i]].empty()) continue;
int p1=q[s[i]].front(); q[s[i]].pop();
int p2=q[-s[i]].front(); q[-s[i]].pop();
// if(p1>p2) swap(p1,p2);
p1+=get(n)-get(p1+1);
p2+=get(n)-get(p2+1);
ans+=abs(p2-p1)-1;
if(p1>=p2) ans++;
add(p2+1,1);
// debug(i,s[i],p1,p2);
}
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... |