传送门

其实有一个显然的性质嘛:对于每个数,其实只要考虑它最右能被换到的位置就好了

然后设\(f[i][j]\)表示已经处理完了前\(i-1\)位,当前还有\(j\)个\(1\)可以自由支配(注意这里说的是当前可以自由支配,不是总共可以自由支配的\(1\))

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=3010,mod=1e9+7;
char ch[maxn];
int n,m,f[maxn][maxn],tot,sum[maxn],las[maxn];
int add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
int main()
{
read(n),read(m);scanf("%s",ch+1);
for(rg int i=1;i<=n+1;i++){
sum[i]=sum[i-1],las[i]=i;
if(ch[i]=='1')tot++,sum[i]++;
}
for(rg int i=1,x,y;i<=m;i++)read(x),read(y),las[x]=max(las[x],y);
for(rg int i=1;i<=n+1;i++)las[i]=max(las[i],las[i-1]);
f[1][sum[las[1]]]=1;
for(rg int i=1;i<=n;i++)
for(rg int j=0;j<=tot;j++)
if(f[i][j]){
int a=las[i],b=las[i+1];
if(j)f[i+1][j+sum[b]-sum[a]-1]=add(f[i+1][j+sum[b]-sum[a]-1],f[i][j]);
if(las[i]+1-i-j)f[i+1][j+sum[b]-sum[a]]=(f[i+1][j+sum[b]-sum[a]],f[i][j]);
}
printf("%d\n",f[n+1][0]);
}

AT2161 シャッフル / Shuffling的更多相关文章

  1. PAT自测-5 Shuffling Machine

    原题连接https://pta.patest.cn/pta/test/17/exam/4/question/264 Shuffling is a procedure used to randomize ...

  2. Shuffling Machine和双向链表

    1. 双向链表 https://github.com/BodhiXing/Data_Structure 2. Shuffling Machine https://pta.patest.cn/pta/t ...

  3. codeforces Gym 100187J J. Deck Shuffling dfs

    J. Deck Shuffling Time Limit: 2   Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...

  4. 【原创】 Shuffling

    在机器学习领域中,经常会听到“shuffling"这个术语.那么,shuffling到底是什么意思呢. 通常,shuffling指的是在SGD怎样依赖训练数据输入顺序的算法中,将训练数据随机 ...

  5. 数据结构练习 00-自测5. Shuffling Machine (20)

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  6. 1042. Shuffling Machine (20) - sstream实现数字转字符串

    题目例如以下: Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffli ...

  7. 自测-5 Shuffling Machine

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  8. PAT 1042. Shuffling Machine (20)

    1042. Shuffling Machine (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shu ...

  9. PAT1042:Shuffling Machine

    1042. Shuffling Machine (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shu ...

随机推荐

  1. Uva 10820 Send a Table(欧拉函数)

    对每个n,答案就是(phi[2]+phi[3]+...+phi[n])*2+1,简单的欧拉函数应用. #include<iostream> #include<cstdio> # ...

  2. visual studio tools for unity代码提示快捷键

    visual studio tools for unity代码提示快捷键  ctrl+ shift +q

  3. [原]NYOJ-公约数和公倍数 -40

    大学生程序代写 //http://acm.nyist.net/JudgeOnline/problem.php?pid=40 公约数和公倍数 时间限制:1000 ms  |  内存限制:65535 KB ...

  4. FEC之我见四

    接上文,来详细的说明一下FEC前向纠错的具体实现: FEC_matrix是一个比较常用的算法,Vandermonde,范德蒙矩阵是法国数学家范德蒙提出的一种各列为几何级数的矩阵. 范德蒙矩阵的定义: ...

  5. ACM学习历程—HDU 5459 Jesus Is Here(递推)(2015沈阳网赛1010题)

    Sample Input 9 5 6 7 8 113 1205 199312 199401 201314 Sample Output Case #1: 5 Case #2: 16 Case #3: 8 ...

  6. poj 3469 Dual Core CPU——最小割

    题目:http://poj.org/problem?id=3469 最小割裸题. 那个限制就是在 i.j 之间连双向边. 根据本题能引出网络流中二元关系的种种. 别忘了写 if ( x==n+1 ) ...

  7. 数据结构-二叉搜索树的js实现

    一.树的相关概念 1.基本概念 子树 一个子树由一个节点和它的后代构成. 节点的度 节点所拥有的子树的个数. 树的度 树中各节点度的最大值 节点的深度 节点的深度等于祖先节点的数量 树的高度 树的高度 ...

  8. asp弹出层

    asp弹出层 <style type="text/css"> html, body { height: %; width: %; } .white_content { ...

  9. <正则吃饺子> :关于mybatis中使用的问题(一)

    在公司项目开始之前,根据springboot .mybatis.Swagger2 整合了一个demo,在测试时候,遇到的问题,简单记录.之前在使用mybatis时候,没有注意到这一点. 1.错误:Th ...

  10. js开发:数组的push()、pop()、shift()和unshift()(转)

    js开发:数组的push().pop().shift()和unshift() 2017-05-18 11:49 1534人阅读 评论(0) 收藏 举报  分类: javascript开发(22)  版 ...