1 solutions

  • 0
    @ 2025-11-10 11:25:52

    金科(rules)

    原题: P8592 不完全一样 [数据删除] 说: “一场比赛不可能没有 dp!” 这道题 100% 是一道 dp!

    设 fif_i 表示当第 ii 条线段为红色时,前 ii 根线段中红色线段的最小长度和 得到状态转移方程:$f_i = \min\limits_{j\atop r_j < l_i} (f_i, f_j + w_i)$

    我们突然意识到:在状态转移前,我们还要判断合不合法,即是不是任意一条黑色线段至少和一条红色线段相交。

    【O(n3)O(n^3)】

    在 dp 的 2 重循环基础上,再增加 1 重循环,用来枚举每根线段,以确定情况合不合法,如果合法就状态转移,不合法就继续。

    【O(n2)O(n^2)】

    由上文可知,现在遇到的难题就是要判断情况是否合法。 既然在 dp 中判断不行,那么预处理这种判断不就行了? 所以我们再建一个数组用来存储预处理的结果:

    设 gi,jg_{i,j} 表示当第 ii 和 jj 根线段都为红色时,中间线段为黑色是否可行。 于是乎只要用 O(n2)O(n^2) 生成 gg 数组,再和之前一样用 O(n2)O(n^2) dp 就行了。

    【O(nlog⁡n)O(n\log n)】

    观察到我们的时间复杂度瓶颈在求 gg 以及 fif_i 上,考虑优化。

    首先我们要知道右端点严格小于 lil_i 的线段中,左端点最大的线段的左端点的位置,记为 bb。看起来很别扭,其实就是:posi=max⁡{lj}rj<lipos_i = \max\{l_j\}_{r_j < l_i}

    这个位置,是前 i−1i-1 条线段中线段 ii 覆盖不到的最后位置。所以对于任意 jj,若 fjf_j 可以更新 fif_i,那么 rj≥posir_j \geq pos_i 是必须的。此时线段 jj 覆盖了 posipos_i,那么由 posipos_i 的定义可知,前 i−1i-1 条线段都已满足条件。又因为两条红色线段不能相交,于是 jj 与 ii 不能相交,即 rj<lir_j < l_i。所以,gj,i=[posi≤rj<li]g_{j,i} = [pos_i \leq r_j < l_i]

    总结一下:fi=min⁡posi≤rj<li{fj}f_i = \min\limits_{pos_i \leq r_j < l_i} \{f_j\} 用两次 rmq 即可,一次求 pospos,一次求 ff。至此,时间复杂度优化至 O(nlog⁡n)O(n\log n)。

    【O(n)O(n)】

    对于 pospos,我们预先记录下每一个右端点对应的左端点,然后对于所有小于 lil_i 的位置求其对应的左端点位置的最大值即为 posipos_i,计算前缀最大值即可。 对于 ff,不难发现 posipos_i 和 lil_i 都单调递增,用单调队列代替线段树即可。

    //Contestants only need to implement the solve() function, and any changes made to other parts will be at their own risk
    #include<bits/stdc++.h> //Universal header file
    using namespace std;
    const int N=5e6+10; //n does not exceed 5e6
    const int M=(1<<22)+10; //m does not exceed 4194303 
    unsigned int n,m,l[N],r[N],w[N]; //Approximately 64MB of space has been occupied
    long long ans; //Note that the answer may exceed the range of unsigned int
    unsigned int seed; //Random seeds
    unsigned int rnd(unsigned int &x); //Random number generator
    inline void input(); //Input data
    inline void output(); //Output the answer, note that the final answer needs to be stored in the variable ans
    inline void solve(); //Calculate the answer
    //Leave the part for contestants to define variables
    unsigned int b[M];
    long long f[M],q[N],ll,rr;
    int tot,head[M],to[N],nxt[N];
    int main(){
        freopen("rules.in","r",stdin); //This question requires opening a file for reading and writing
        freopen("rules.out","w",stdout);
        input();
        solve();
        output();
    	return 0;
    }
    inline void add(int u, int v) {
    	tot++;
    	nxt[tot]=head[u];
    	to[tot]=v;
    	head[u]=tot;
    	return;
    }
    inline void solve() { //Contestants only need to implement this function
    	//Note that the following code can only pass test 1. Contestants pursuing higher scores need to delete the following three lines
    	/*for(int i = 1; i <= n; i++) {
            ans += w[i];
        }*/
        m++;
        for(int i=1;i<=n;i++) {
        	l[i]++;
        	r[i]++;
    	}
        n++;m++;
    	l[n]=m;r[n]=m;w[n]=0;
    	for(int i=1;i<=m;i++){
    		b[i]=0;
    		f[i]=0x3f3f3f3f3f3f3f3f;
    	}
    	for(int i=1;i<=n;i++){
    		b[r[i]]=max(b[r[i]],l[i]);
    		add(l[i],i);
    	}
    	ll=1;rr=1;q[1]=0;f[0]=0;
    	for(int i=1;i<=m;i++){
    		while(ll<=rr&&q[ll]<b[i-1])ll++;
    		for(int k=head[i];k;k=nxt[k]){
    			int j=to[k];
    			f[r[j]]=min(f[r[j]],f[q[ll]]+w[j]);
    		}
    		while(ll<=rr&&f[q[rr]]>f[i])rr--;
    		q[++rr]=i;
    	}
    	ans=f[m];
    	return;
    }
    unsigned int rnd(unsigned int &x) { 
        x ^= (x << 13) & 0xFFFFFFF;
        x ^= (x >> 17);
        x ^= (x << 5) & 0xFFFFFFF;
        x += 100;
        return x;
    }
    inline void input() {
        scanf("%u%u%u", &n, &m, &seed);
        for (int i = 1; i <= n; i++) {
            l[i] = rnd(seed) & m;
            r[i] = rnd(seed) & m;
            w[i] = rnd(seed);
            if (l[i] > r[i]) {
                swap(l[i], r[i]);
            }
        }
        return;
    }
    inline void output() {
    	printf("%lld\n", ans);
    	return;
    }
    
    • 1

    Information

    ID
    133
    Time
    2000ms
    Memory
    512MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    28
    Accepted
    1
    Uploaded By