Submission #1000329

#TimeUsernameProblemLanguageResultExecution timeMemory
1000329MateiKing80Monochrome Points (JOI20_monochrome)C++14
100 / 100
1030 ms15568 KiB
#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 + 5);
    vector<pair<int, int>> v;
    for(int i = 0; i < n; i ++)
        v.push_back({w[i], b[(i + k) % n]});
    for(int i = 0; i < n; i ++)
        if(v[i].first > v[i].second)
            swap(v[i].first, v[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 << 20), pos = -1;
    while(pas)
    {
        if(nrInv(pos + pas) <= nrInv(pos + pas + 1))
            pos += pas;
        pas /= 2;
    }
    cout << nrInv(pos + 1);
}

Compilation message (stderr)

monochrome.cpp: In member function 'void AIB::update(ll, ll)':
monochrome.cpp:24:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |         while(pos < aib.size())
      |               ~~~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...