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
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
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
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
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
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;
}