이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=400005;
vector<ll>ar(N,0);
ll get(int p){
ll ans=0;
while(p){
ans+=ar[p];
p-=p&(-p);
}
return ans;
}
void add(int p,ll val){
while(p<N){
ar[p]+=val;
p+=p&(-p);
}
}
long long count_swaps(std::vector<int> s) {
vector<pair<int,int>>g[N];
ar= vector<ll>(N,0);
int n=s.size()/2;
for(int i=0;i<s.size();i++){
g[abs(s[i])].push_back({s[i],i});
add(i+1,1);
}
ll ans=0;
vector<pair<int,int>>ns;
for(int i=1;i<=n;i++){
sort(g[i].begin(),g[i].end());
int cnt=g[i].size()/2;
for(int j=0;j<cnt;j++){
int l=g[i][j].second;
int r=g[i][j+cnt].second;
if(l>r){
ans++;
swap(l,r);
}
ns.push_back({l+1,r+1});
}
}
sort(ns.begin(),ns.end());
for(auto el:ns){
int l=el.first,r=el.second;
ans+=get(r-1)+get(l);
add(l,-1);add(r,-1);
}
return ans;
}
/*int main(){
vector<int>ar={2,1,-1,-2};
for(auto el:ar)cout<<el<<endl;
count_swaps(ar);
return 0;
}*/
컴파일 시 표준 에러 (stderr) 메시지
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:25:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int i=0;i<s.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... |