이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
struct binary_indexed_tree{
int N;
vector<int> BIT;
binary_indexed_tree(int N): N(N), BIT(N + 1, 0){
}
void add(int i, int x){
i++;
while (i <= N){
BIT[i] += x;
i += i & -i;
}
}
int sum(int i){
int ans = 0;
while (i > 0){
ans += BIT[i];
i -= i & -i;
}
return ans;
}
int sum(int L, int R){
return sum(R) - sum(L);
}
};
bool compare(vector<int> &a, vector<int> &b, int x, int y){
if (a[x] < a[y] && a[y] < b[x] && b[x] < b[y]){
return true;
}
if (a[y] < a[x] && a[x] < b[y] && b[y] < b[x]){
return false;
}
if (b[x] < a[y]){
return true;
}
if (b[y] < a[x]){
return false;
}
return x < y;
}
void merge_sort(vector<int> &A, vector<int> &a, vector<int> &b, int l, int r){
if (r - l == 1){
return;
}
int m = (l + r) / 2;
merge_sort(A, a, b, l, m);
merge_sort(A, a, b, m, r);
vector<int> ans;
int p1 = l, p2 = m;
while (!(p1 == m && p2 == r)){
if (p1 == m){
ans.push_back(A[p2]);
p2++;
} else if (p2 == r){
ans.push_back(A[p1]);
p1++;
} else if (compare(a, b, A[p1], A[p2])){
ans.push_back(A[p1]);
p1++;
} else {
ans.push_back(A[p2]);
p2++;
}
}
for (int i = 0; i < r - l; i++){
A[l + i] = ans[i];
}
}
long long count_swaps(vector<int> S){
int n = S.size() / 2;
vector<int> a, b, x;
map<int, vector<int>> mp1;
map<int, int> mp2;
for (int i = 0; i < n * 2; i++){
if (mp1[-S[i]].size() == mp2[-S[i]]){
mp1[S[i]].push_back(i);
} else {
a.push_back(mp1[-S[i]][mp2[-S[i]]]);
b.push_back(i);
x.push_back(abs(S[i]));
mp2[-S[i]]++;
}
}
vector<int> A(n);
for (int i = 0; i < n; i++){
A[i] = i;
}
merge_sort(A, a, b, 0, n);
vector<int> S2;
for (int i = 0; i < n; i++){
S2.push_back(-x[A[i]]);
S2.push_back(x[A[i]]);
}
map<int, deque<int>> Q2;
for (int i = 0; i < n * 2; i++){
Q2[S2[i]].push_back(i);
}
vector<int> p;
for (int i = 0; i < n * 2; i++){
p.push_back(Q2[S[i]].front());
Q2[S[i]].pop_front();
}
long long ans = 0;
binary_indexed_tree BIT(n * 2);
for (int i = 0; i < n * 2; i++){
ans += BIT.sum(p[i] + 1, n * 2);
BIT.add(p[i], 1);
}
return ans;
}
컴파일 시 표준 에러 (stderr) 메시지
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:77:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'std::map<int, int>::mapped_type' {aka 'int'} [-Wsign-compare]
77 | if (mp1[-S[i]].size() == mp2[-S[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... |