이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#include<bits/stdc++.h>
using namespace std;
int N;
void change(int index, int delta, int BIT[]){
for(; index <N; index += (index&(-index))){
BIT[index]+=delta;
}
}
int query(int index, int BIT[]){
int ans = 0;
for(; index > 0; index -= (index&(-index))) ans+= BIT[index];
return ans;
}
long long count_swaps(vector<int> s) {
map<int, vector<int>> M;
N = s.size()+1;
int BIT[N];
memset(BIT, 0, sizeof(BIT));
for(int i = 1; i < N; ++i) change(i, 1, BIT);
long long ans = 0;
for(int i = 0; i < s.size(); ++i){
//cout << i << " " << ans << endl;
if(M.find(-s[i]) == M.end()){
if(M.find(s[i]) == M.end()){
M[s[i]] = {i};
}
else if(M[s[i]].empty()){
M[s[i]] = {i};
}
else{
M[s[i]].push_back(i);
}
}
else if(M[-s[i]].empty()){
if(M.find(s[i]) == M.end()){
M[s[i]] = {i};
}
else if(M[s[i]].empty()){
M[s[i]] = {i};
}
else{
M[s[i]].push_back(i);
}
}
else{
if(s[i] < 0) ans++;
//cout << "a ";
int prev = M[-s[i]].back();
//cout <<"b ";
M[-s[i]].pop_back();
//cout <<"c ";
ans += (long long)query(i, BIT)-(long long)query(prev+1, BIT);
//cout <<"d ";
change(i+1, -1, BIT);
//cout << "e ";
change(prev+1, 1, BIT);
}
}
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:27:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | 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... |