이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#define pb push_back
#define ll long long
using namespace std;
const int N = 100005;
vector <int> lf[N],rg[N];
bool f[N];
ll fen[N];
pair<int,pair<int,int> > d[N];
void upd(int ind,int val){
while(ind <= N){
fen[ind] += val;
ind += (ind&(-ind));
}
}
ll get(int ind){
ll s=0;
while(ind){
s += fen[ind];
ind -= (ind&(-ind));
}
return s;
}
ll count_swaps(vector <int> a) {
for (int i = 0; i < a.size(); i++){
if (a[i] < 0) lf[-a[i]].pb(i);
else rg[a[i]].pb(i);
}
ll cnt = 0;
for (int i = 0; i < a.size(); i++){
if (f[i]) continue;
if (a[i] < 0){
int ind = rg[-a[i]][0];
f[ind] = 1;
rg[-a[i]].erase(rg[-a[i]].begin());
d[++cnt] = {abs(ind - i),{i,ind}};
lf[-a[i]].erase(lf[-a[i]].begin());
}
else{
int ind = lf[a[i]][0];
f[ind] = 1;
lf[a[i]].erase(lf[a[i]].begin());
d[++cnt] = {abs(ind - i),{ind,i}};
rg[a[i]].erase(rg[a[i]].begin());
}
}
sort(d + 1,d + cnt + 1);
ll ans=0;
for (int i = 1; i <= cnt; i++){
int l = d[i].second.first,r = d[i].second.second;
if (l > r) swap(l,r);
ans += r - l - 1 + get(r + 1) - get(l);
if (a[l] > 0 && a[r] < 0) ans++;
upd(l + 1, -1);
upd(r + 1, -1);
}
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:32:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int i = 0; i < a.size(); i++){
| ~~^~~~~~~~~~
shoes.cpp:38:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int i = 0; i < a.size(); i++){
| ~~^~~~~~~~~~
# | 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... |