이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
typedef long long ll;
typedef pair<int,int> pii;
int n, mark[maxn], bit[maxn];
ll sum(int x){
ll res = 0;
while(x>0){
res += bit[x];
x -= (x&-x);
}
return res;
}
int upd(int x, int v){
while(x<=n){
bit[x] += v;
x += (x&-x);
}
}
ll count_swaps(vector<int> s){
n = s.size();
ll ans = 0LL;
vector<pii> l, r;
for(int i=0; i<n; i++){
mark[abs(s[i])]++;
if(mark[abs(s[i])]&1){
l.push_back({abs(s[i]), i});
if(s[i] > 0) ans++;
}
else{
r.push_back({abs(s[i]), i});
}
}
sort(l.begin(), l.end());
sort(r.begin(), r.end());
for(int i=0; i<l.size(); i++){
r[i].first = l[i].second;
l[i].first = l[i].second;
}
sort(l.begin(), l.end());
sort(r.begin(), r.end());
int tt = 0;
for(int i=0; i<l.size(); i++){
s[l[i].second] = ++tt;
s[r[i].second] = ++tt;
}
for(int i=0; i<n; i++){
upd(s[i], 1);
ans += sum(n) - sum(s[i]);
}
return ans;
}
/*int main(){
int n;
cin >> n;
vector<int> s;
while(n--){
int x;
cin >> x;
s.push_back(x);
}
cout << count_swaps(s) << endl;
}*/
컴파일 시 표준 에러 (stderr) 메시지
shoes.cpp: In function 'int upd(int, int)':
shoes.cpp:26:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
shoes.cpp: In function 'll count_swaps(std::vector<int>)':
shoes.cpp:49:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<l.size(); i++){
~^~~~~~~~~
shoes.cpp:59:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<l.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... |