이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
//#include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
const int MAXN=(1<<17);
int tree[2*MAXN-1]={0};
int find(int x, int l, int r, int lt, int rt)
{
if (l>=rt || r<=lt)
return 0;
if (l>=lt && r<=rt)
return tree[x];
return find(2*x+1, l, (l+r)/2, lt, rt)+find(2*x+2, (l+r)/2, r, lt, rt);
}
void update(int x, int l, int r, int index, int value)
{
if (l>index || r<=index)
return;
tree[x]=tree[x]+value;
if (r-l==1)
return;
update(2*x+1, l, (l+r)/2, index, value);
update(2*x+2, (l+r)/2, r, index, value);
return;
}
long long count_swaps(vector<int> a) {
int n=a.size()/2;
vector<int> left[n+1], right[n+1];
for (int i=0; i<2*n; i++)
{
if (a[i]<0)
left[-a[i]].push_back(i);
else
right[a[i]].push_back(i);
}
int match[2*n];
long long total=0;
for (int i=1; i<=n; i++)
for (int j=0; j<left[i].size(); j++)
{
match[min(left[i][j], right[i][j])]=max(left[i][j], right[i][j]);
match[max(left[i][j], right[i][j])]=-1;
if (right[i][j]<left[i][j])
total=total+1;
}
for (int i=0; i<2*n; i++)
{
if (match[i]==-1)
continue;
total=total+match[i]-i-1;
total=total-find(0, 0, MAXN, i, match[i]);
update(0, 0, MAXN, match[i], 1);
}
return total;
}
컴파일 시 표준 에러 (stderr) 메시지
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:48:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int j=0; j<left[i].size(); j++)
| ~^~~~~~~~~~~~~~~
# | 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... |