# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
999911 | MateiKing80 | Monochrome Points (JOI20_monochrome) | C++14 | 1 ms | 348 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;
struct AIB
{
vector<int> aib;
AIB(int N)
{
aib.resize(N + 1, 0);
}
inline int lsb(int x)
{
return x & -x;
}
void update(int pos, int val)
{
while(pos < aib.size())
aib[pos] += val, pos += lsb(pos);
}
int query(int pos)
{
int ans = 0;
while(pos)
ans += aib[pos], pos -= lsb(pos);
return ans;
}
};
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.update(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 << 30), 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... |