QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#524895#7937. Fast XORtingnathan4690TL 0ms11076kbC++172.0kb2024-08-20 10:08:272024-08-20 10:08:28

Judging History

你现在查看的是最新测评结果

  • [2024-08-20 10:08:28]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:11076kb
  • [2024-08-20 10:08:27]
  • 提交

answer

// Fast XORting
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define el cout << '\n'
#define f1(i,n) for(ll i=1;i<=n;i++)
#define __file_name ""
using namespace std;
const ll maxn = 1e6+5, inf=1e18;

struct FenwickTree{
    ll n;
    vector<ll> bit;
    FenwickTree(ll n): n(n), bit(n+1, 0ll){};
    void update(ll idx, ll v){for(;idx<=n;idx+=idx&-idx) bit[idx] += v;}
    ll getSum(ll idx){
        ll res = 0;
        for(;idx>0;idx-=(idx&-idx)) res += bit[idx];
        return res;
    }
};

ll n,a[maxn],ans;
vector<ll> all;
bool flag;

ll solve(const vector<ll> &v, ll bit, ll val){
    ll ans = 0;
    ll c0 = 0, a0 = 0, c1 = 0, a1 = 0;
    vector<ll> v0, v1;
    for(ll i=v.size()-1;i>=0;i--){
        ll item = v[i];
        if(((item & (1 << bit)) != val)) {
            v1.push_back(item);
            c1++;
            a1 += c0;
        }else {
            v0.push_back(item);
            c0++;
        }
    }
    reverse(v0.begin(), v0.end());
    reverse(v1.begin(), v1.end());
    // cout << bit << ' ' << a0 << ' ' << a1;el;
    ans += a1;
    if(bit){
        ll vv0 = solve(v0, bit-1, 0) + solve(v1, bit-1, 0);
        ll vv1 = solve(v0, bit-1, 1) + solve(v1, bit-1, 1);
        // if(vv1 > vv0) flag = false;
        ans += min(vv0, vv1);
    }
    return ans;
}

FenwickTree fen(maxn);

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    if(fopen(__file_name ".inp", "r")){
        freopen(__file_name ".inp","r",stdin);
        freopen(__file_name ".out","w",stdout);
    }
    // code here
    cin >> n;
    f1(i,n) {
        cin >> a[i];
        // cout << bitset<3>(a[i]).to_string();el;
    }
    ll preans = 0;
    for(ll i=n;i>=1;i--){
        preans +=fen.getSum(a[i]);
        fen.update(a[i] + 1, 1);
    }
    f1(i,n) all.push_back(a[i]);
    flag = true;
    ll vv0 = solve(all, __lg(n) - 1, 0);
    ll vv1 = solve(all, __lg(n) - 1, 1);
    // if(vv1 > vv0) flag = false;
    cout << min(min(vv0, vv1) + 1, preans);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 11076kb

input:

8
0 1 3 2 5 4 7 6

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 0ms
memory: 10892kb

input:

8
2 0 1 3 4 5 6 7

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: -100
Time Limit Exceeded

input:

262144
47482 131703 90418 122675 166494 247529 196154 16950 66501 50357 246808 25929 10418 50538 26955 151884 63776 58023 20073 26544 74785 44064 41836 148543 87920 54172 3270 131495 130960 112122 167229 215767 77499 195004 21391 11039 168999 256346 109690 180904 172679 157200 78594 201857 52784 147...

output:


result: