zoukankan      html  css  js  c++  java
  • cf1539E. Game with Cards

    题目描述

    题解

    姜老师推荐的套路题。

    考虑到交换,所以我们可以在交换的时刻做个dp。

    即考虑 $f_{i,0/1}$ 表示能否在第 $i$ 个时刻为 $0/1$ ,在 $i+1$ 为 $1/0$ 。

    考虑 $f_{i,0}$ 会被 $f_{j,1}$ 更新,要满足 $a_i$ 符合 $[i+1,j]$ 的 $0$ 的区间且 $[i+1,j]$ 中的每个 $a_k$ 都在其 $1$ 的区间内。

    故我们只要知道最小的 $j$ 满足 $f_{j,1}=1$ 即可,然后用单调队列判断 $a_i$ 是否在区间内即可。

    $f_{i,1}$ 同理,最后我们看 $f_{0,0/1}$ 是否至少有一个为 $1$ 即可。然后记录一下转移路径。

    效率: $O(n)$ 。

    代码

    #include <bits/stdc++.h>
    using namespace std;
    const int N=1e5+5;
    int n,m,a[N],L[2][N],R[2][N],f[2][N],q[2][2][N],l[2][2],r[2][2],h[2],d[2],p[2][N];
    int main(){
        scanf("%d%d",&n,&m);
        for (int i=1;i<=n;i++)
            scanf("%d%d%d%d%d",&a[i],&L[0][i],&R[0][i],&L[1][i],&R[1][i]);
        f[0][n]=f[1][n]=l[0][0]=l[1][0]=l[0][1]=l[1][1]=1;
        for (int i=n;i;i--){
            for (int j=0;j<2;j++){
                if (a[i]<L[!j][i] || a[i]>R[!j][i]) d[j]=0;
                else if (!d[j]) d[j]=i;
                while(r[j][0]>=l[j][0] && L[j][q[j][0][r[j][0]]]<=L[j][i]) r[j][0]--;
                q[j][0][++r[j][0]]=i;
                while(r[j][1]>=l[j][1] && R[j][q[j][1][r[j][1]]]>=R[j][i]) r[j][1]--;
                q[j][1][++r[j][1]]=i;
                if (f[!j][i]) h[j]=i;
                if (!h[j]) continue;
                while(l[j][0]<=r[j][0] && q[j][0][l[j][0]]>h[j]) l[j][0]++;
                while(l[j][1]<=r[j][1] && q[j][1][l[j][1]]>h[j]) l[j][1]++;
                if (h[j]>d[j]) continue;
                if (L[j][q[j][0][l[j][0]]]<=a[i-1] && a[i-1]<=R[j][q[j][1][l[j][1]]])
                    f[j][i-1]=1,p[j][i-1]=h[j];
            }
        }
        if (!f[0][0] && !f[1][0]) puts("No");
        else{
            puts("Yes");
            int u=f[0][0];
            for (int i=1,j=p[!u][0];i<=n;i=j+1,j=p[u][j],u^=1)
                for (int k=i;k<=j;k++)
                    printf("%d%c",u,k<n?' ':'\n');
        }
        return 0;
    }
  • 相关阅读:
    一篇文章看懂mysql中varchar能存多少汉字、数字,以及varchar(100)和varchar(10)的区别
    SQL处理下划线分割的两边数字都分别增加值
    [LeetCode]Binary Tree Zigzag Level Order Traversal
    [LeetCode]Binary Tree Level Order Traversal
    [LeetCode]Candy
    [LeetCode]Single Number II
    [LeetCode]Single Number
    [LeetCode]Copy List with Random Pointer
    [LeetCode]Link List Cycle II
    [LeetCode]Link List Cycle
  • 原文地址:https://www.cnblogs.com/xjqxjq/p/15539627.html
Copyright © 2011-2022 走看看