SRM 664 hard BearSorts题解搬运

Div1 Hard: BearSorts

First, we must understand what is probability of getting some sequence. It is equal to 0.5k where k is number of comparisons made by mergesort to get this sequence. To see why, you can check out Div2 Hard editorial. From now on, calculating/specified/given probability will mean calculating/specified/given number of comparisons. Fortunately, we don’t have to care about precision issues because we can keep probabilities as integers. And we will use a fact that mergesort does at most nlogn comparisons (otherwise, mergesort would have bigger complexity).

We are given permutation P and asked to count permutations with greater probability and those lexicographically smaller with the same probability. To do second part (btw. it will do first part too), we need a function forPrefix(pref, probability) which calculates number of permutations starting with specified prefix and probability. It turns out, that to make whole solution efficient, it’s better to have function forPrefix(pref) which calculates number of permutations for every possible probability. If you don’t see how to use this function to solve a task, check it in code below. Here we will focus on writing forPrefix(pref) function which will be called O(N2) times. We will write this function in O(N2log2N) to get total complexity O(N4log2N) .

Mergesort with random LESS() returns permutation with prefix pref iff the following is true. If at least one of compared numbers a,b is in pref , then LESS(a,b) returns specified value, not leading to contradiction. If only a is in pref, it returns true. If only b is in pref, false. If both a and b are in pref , returned value must depend on “ a is before b in pref ”. Otherwise, it could return any value.

Function forPrefix(pref) will run mergesort simulation. In every recursive call of mergesort we merge two arrays, sorted previously (recursively). These two arrays are guaranteed to have all elements from pref as their prefixes (let’s call them special prefixes). After merging we want to again produce array with all elements from pref before other elements. First, we merge special prefixes in one way, specified by order of numbers in pref . Then we must merge other elements in every possible way.

When merging, one array will run out of numbers first and then we get some skipped suffix of the other array. In true mergesort such a suffix contains numbers bigger than all numbers in the other array. Let’s say we both arrays have sizes n1 and n2 and special prefixes p1 and p2 . Let’s say we skip suffix of size x in first array. Then there is C(n1p1x,n2p2) ways (binomial coefficient) to merge arrays (we need to fix order of elements from middles). And we have n1+n2x comparisons. For both arrays we should iterate over size x of skipped suffix.

We can treat every resursive call of mergesort separately, because order of elements in halves doesn’t affect merging them. Every call produces some array of numbers of ways to achieve some probabilities, in code below I keep it globally in vector RES. We must multiply all those arrays like in FFT but we can do it slower. Total size of those arrays is limited by total number of comparisons which is O(NlogN) , so indeed we can do it with brute force with total complexity O(N2log2N) .

About implementation below. RES is result of multiplied arrays of numbers of ways to achieve probabilities. consider(vector) adds something to RES .

#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i = (a); i ≤ (b); ++i)
typedef long long ll;
const int mod = 1e9 + 7;

vector<int> RES;
void consider (vector<int> w) { // equivalent to FFT
    assert(w.back() > 0 && w[0] == 0);
    int memo_size = (int) RES.size();
    RES.resize(memo_size + (int) w.size() - 1, 0);
    for(int i = memo_size - 1; i ≥ 0; --i) {
        FOR(j, 0, (int) w.size() - 1)
            RES[i+j] = (RES[i+j] + (ll) RES[i] * w[j]) % mod;
        RES[i] = 0;
    }
}

const int nax = 105;
int binom[nax][nax];
bool smaller[nax][nax];

void mergeSort(int a, int d) { // numbers in [a, d], inclusive
    if(a == d) return;
    int n = d-a+1;
    int c = a+n/2;
    int b = c-1;
    mergeSort(a, b);
    mergeSort(c, d);
    vector<int> L, R;
    FOR(i,a,b) L.push_back(i);
    FOR(i,c,d) R.push_back(i);
    sort(L.begin(), L.end(), [](int a, int b){return smaller[a][b];});
    sort(R.begin(), R.end(), [](int a, int b){return smaller[a][b];});
    int iL = 0, iR = 0;
    int r = 0;
    while(iL < (int)L.size() && iR < (int)R.size()
            && (smaller[L[iL]][R[iR]] || smaller[R[iR]][L[iL]])) {
        ++r;
        if(smaller[L[iL]][R[iR]]) ++iL;
        else ++iR;
    }
    if(iL ≥ (int)L.size() || iR ≥ (int)R.size()) {
        vector<int> w(r+1, 0);
        w[r] = 1;
        consider(w);
        return;
    }
    vector<int> w;
    int n1 = (int)L.size() - iL, n2 = (int)R.size() - iR;
    FOR(repeat, 0, 1) {
        // let's say we'll run out of numbers in L, i.e. the biggest number is in R
        // let's say there are x numbers in R bigger than everything in L
        FOR(x, 1, n2) {
            int one = n1-1; // everything but the biggest one
            int two = n2 - x;
            while((int)w.size() ≤ n-x) w.push_back(0);
            w[n-x] += binom[one+two][one];
        }
        swap(n1, n2); // to avoid copy-paste
    }
    consider(w);
}

// for given prefix (e.g. {2,5,1,6,0,0}) calculates
// global vector RES with number of ways to achieve each probability
void forPrefix(vector<int> pref) {
    int n = (int) pref.size();
    FOR(i,1,n) FOR(j,1,n) smaller[i][j] = false;
    FOR(i,0,n-1) if(pref[i]) {
        int a = pref[i];
        FOR(b,1,n) if(a != b && !smaller[b][a]) smaller[a][b] = true;
    }
    RES = vector<int>({1});
    mergeSort(1, n);
}

int index(vector<int> w) {
    FOR(i, 0, nax-1) binom[i][0] = 1;
    FOR(i,0,nax-1) FOR(j,1,i) binom[i][j] = (binom[i-1][j-1] + binom[i-1][j]) % mod;
    forPrefix(w);
    int me = (int) RES.size() - 1;
    int result = 1;
    // with greater probability (smaller number of comparisons)
    forPrefix(vector<int>((int)w.size(),0)); // empty prefix, vector with N zeros
    FOR(i,0,me-1) result = (result + RES[i]) % mod;
    // with the same probability, harder part
    for(int i = (int) w.size() - 1; i ≥ 0; --i) while(w[i]) {
        w[i]--;
        if(w[i] == 0) continue;
        bool ok = true;
        FOR(j,0,i-1) if(w[j] == w[i]) ok = false;
        if(!ok) continue;
        forPrefix(w);
        if(me < (int) RES.size()) result = (result + RES[me]) % mod;
    }
    return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值