# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1000325 | MateiKing80 | Monochrome Points (JOI20_monochrome) | C++14 | 1 ms | 436 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
string s;
vector<int> w, b;
#define lsb(x) (x & -x)
struct AIB
{
vector<int> tree;
AIB(int n)
{
tree.assign(n + 5, 0);
}
void upd(int p, int x)
{
while(p < tree.size())
tree[p] += x, p += lsb(p);
}
int query(int p)
{
int sum = 0;
while(p > 0)
sum += tree[p], p -= lsb(p);
return sum;
}
};
int n;
int nrInv(int k)
{
int ans = 0;
AIB ds(2 * n);
vector<pair<int, int>> v;
for(int i = 0; i < n; i ++)
v.push_back({w[i], b[(i + k) % n]});
for(auto &i : v)
if(i.first > i.second)
swap(i.first, i.second);
sort(v.begin(), v.end());
for(int i = 0; i < n; i ++)
{
//cout << v[i].first << " " << v[i].second << "\n";
ans += ds.query(v[i].second) - ds.query(v[i].first);
ds.upd(v[i].second, 1);
}
//cout << '\n';
return ans;
}
signed main()
{
cin >> n >> s;
for(int i = 0; i < 2 * n; i ++)
{
if(s[i] == 'B')
b.push_back(i + 1);
else
w.push_back(i + 1);
}
int pas = (1 << 20), pos = 0;
while(pas)
{
if(pos + pas < n && nrInv(pos + pas - 1) < nrInv(pos + pas))
pos += pas;
pas /= 2;
}
cout << nrInv(pos);
}
Compilation message (stderr)
# | 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... |