이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "shoes.h"
#define startt ios_base::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
using namespace std;
#define vint vector<int>
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define MOD2 998244353
#define MX 1000000000
#define MXL 1000000000000000000
#define PI (ld)2*acos(0.0)
#define pb push_back
#define sc second
#define fr first
//#define int long long
//#define endl '\n'
#define ld long double
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
queue<int> q1[100005], q2[100005];
struct FenwickTree {
vector<int> bit; // binary indexed tree
int n;
FenwickTree(int n) {
this->n = n;
bit.assign(n, 0);
}
FenwickTree(vector<int> a) : FenwickTree(a.size()) {
for (size_t i = 0; i < a.size(); i++)
add(i, a[i]);
}
int sum(int r) {
int ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
ret += bit[r];
return ret;
}
int sum(int l, int r) {
return sum(r) - sum(l - 1);
}
void add(int idx, int delta) {
for (; idx < n; idx = idx | (idx + 1))
bit[idx] += delta;
}
};
long long count_swaps(vector<int> s)
{
int n = s.size();
int cnt = -1;
for(int i = 0; i < n; i++)
{
if(s[i] < 0)
{
cnt+=2;
s[i] = cnt;
q2[-s[i]].push(cnt);
}
}
for(int i = 0; i < n; i++)
{
if(s[i] > 0)
{
int prev = s[i];
s[i] = q2[s[i]].front()+1;
q2[prev].pop();
}
}
ll ans = 0;
FenwickTree ft(n+15);
for(int i = 0; i < n; i++)
{
ans+=ft.sum(s[i] + 1, n+1);
ft.add(s[i], 1);
}
return ans;
}
/*
int main()
{
int n;
cin >> n;
vint a(2*n);
for(int i = 0; i < 2*n; i++)
{
cin >> a[i];
}
cout << count_swaps(a);
}
*/
/*
3
-2 -2 -1 2 1 2
-2 -2 2 -1 1 2
*/
컴파일 시 표준 에러 (stderr) 메시지
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
66 | q2[-s[i]].push(cnt);
| ~~~~~~~~^
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
shoes.cpp:66:12: warning: array subscript -1 is below array bounds of 'std::queue<int> [100005]' [-Warray-bounds]
# | 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... |