이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(),s.end()
//#define int long long
using namespace std;
const int N = (1e5 + 12);
const int mod = (1e9 + 7);
const int INF = (0x3f3f3f3f);
int a[N], minus[N], used[N], t[N * 4];
set<int > s[2][N];
set<int > :: iterator it;
int n;
void update(int pos, int new_val, int v = 1,int tl = 1,int tr = n){
if(tl == tr){
t[v] = new_val;
}
else{
int mid = (tl + tr) >> 1;
if(mid >= pos){
update(pos, new_val, v + v, tl, mid);
}
else{
update(pos, new_val, v + v + 1, mid + 1, tr);
}
t[v] = t[v + v] + t[v + v + 1];
}
}
int get(int l,int r, int v = 1,int tl = 1,int tr = n){
if(tl > r || tr < l){
return 0;
}
if(l <= tl && r >= tr){
return t[v];
}
int mid = (tl + tr) >> 1;
return (get(l, r, v + v, tl, mid) + get(l, r, v + v + 1, mid + 1, tr));
}
long long count_swaps(vector<int> s1)
{
for(int i = 1;i <= n; i++){
a[i] = s1[i - 1];
if(a[i] < 0){
s[0][abs(a[i])].insert(i);
}
else{
s[1][a[i]].insert(i);
}
}
long long ans = 0, plus = 0;
for(int i = 1;i <= n; i++){
if(used[i]){
continue;
}
if(a[i] < 0){
it = s[1][abs(a[i])].begin();
s[1][abs(a[i])].erase(s[1][abs(a[i])].begin());
s[0][abs(a[i])].erase(s[0][abs(a[i])].begin());
ans += *it - i - 1;
ans -= get(i, *it);
update(*it, 1);
used[*it] = 1;
}
else{
it = s[0][a[i]].begin();
s[0][a[i]].erase(s[0][a[i]].begin());
s[1][abs(a[i])].erase(s[1][abs(a[i])].begin());
ans += *it - i;
ans -= get(i, *it);
update(*it, 1);
used[*it] = 1;
}
}
return ans;
}
/*
6
-2 2 -2 2 -2 2
*/
컴파일 시 표준 에러 (stderr) 메시지
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:58:21: warning: unused variable 'plus' [-Wunused-variable]
long long ans = 0, plus = 0;
^~~~
# | 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... |