SRM 664 hard BearSorts题解搬运

本文深入探讨了BearSort问题中概率的计算方法,通过分析MergeSort的比较次数来确定不同序列出现的概率。文中介绍了一个用于计算特定前缀下所有可能序列及其对应概率的高效算法,并详细解释了其实现细节。

Div1 Hard: BearSorts

First, we must understand what is probability of getting some sequence. It is equal to 0.5kwhere 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;
}
内容概要:本文档是一份关于交换路由配置的学习笔记,系统地介绍了网络设备的远程管理、交换机与路由器的核心配置技术。内容涵盖Telnet、SSH、Console三种远程控制方式的配置方法;详细讲解了VLAN划分原理及Access、Trunk、Hybrid端口的工作机制,以及端口镜像、端口汇聚、端口隔离等交换技术;深入解析了STP、MSTP、RSTP生成树协议的作用与配置步骤;在路由部分,涵盖了IP地址配置、DHCP服务部署(接口池与全局池)、NAT转换(静态与动态)、静态路由、RIP与OSPF动态路由协议的配置,并介绍了策略路由和ACL访问控制列表的应用;最后简要说明了华为防火墙的安全区域划分与基本安全策略配置。; 适合人群:具备一定网络基础知识,从事网络工程、运维或相关技术岗位1-3年的技术人员,以及准备参加HCIA/CCNA等认证考试的学习者。; 使用场景及目标:①掌握企业网络中常见的交换与路由配置技能,提升实际操作能力;②理解VLAN、STP、OSPF、NAT、ACL等核心技术原理并能独立完成中小型网络搭建与调试;③通过命令示例熟悉华为设备CLI配置逻辑,为项目实施和故障排查提供参考。; 阅读建议:此笔记以实用配置为主,建议结合模拟器(如eNSP或Packet Tracer)动手实践每一条命令,对照拓扑理解数据流向,重点关注VLAN间通信、路由选择机制、安全策略控制等关键环节,并注意不同设备型号间的命令差异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值