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;
}
基于STM32 F4的永磁同步电机无位置传感器控制策略研究内容概要:本文围绕基于STM32 F4的永磁同步电机(PMSM)无位置传感器控制策略展开研究,重点探讨在不依赖物理位置传感器的情况下,如何通过算法实现对电机转子位置和速度的精确估计与控制。文中结合嵌入式开发平台STM32 F4,采用如滑模观测器、扩展卡尔曼滤波或高频注入法等先进观测技术,实现对电机反电动势或磁链的估算,进而完成无传感器矢量控制(FOC)。同时,研究涵盖系统建模、控制算法设计、仿真验证(可能使用Simulink)以及在STM32硬件平台上的代码实现与调试,旨在提高电机控制系统的可靠性、降低成本并增强环境适应性。; 适合人群:具备一定电力电子、自动控制理论基础和嵌入式开发经验的电气工程、自动化及相关专业的研究生、科研人员及从事电机驱动开发的工程师。; 使用场景及目标:①掌握永磁同步电机无位置传感器控制的核心原理与实现方法;②学习如何在STM32平台上进行电机控制算法的移植与优化;③为开发高性能、低成本的电机驱动系统提供技术参考与实践指导。; 阅读建议:建议读者结合文中提到的控制理论、仿真模型与实际代码实现进行系统学习,有条件者应在实验平台上进行验证,重点关注观测器设计、参数整定及系统稳定性分析等关键环节。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值