AT2161 シャッフル / Shuffling
传送门
其实有一个显然的性质嘛:对于每个数,其实只要考虑它最右能被换到的位置就好了
然后设\(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的更多相关文章
- PAT自测-5 Shuffling Machine
原题连接https://pta.patest.cn/pta/test/17/exam/4/question/264 Shuffling is a procedure used to randomize ...
- Shuffling Machine和双向链表
1. 双向链表 https://github.com/BodhiXing/Data_Structure 2. Shuffling Machine https://pta.patest.cn/pta/t ...
- codeforces Gym 100187J J. Deck Shuffling dfs
J. Deck Shuffling Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/pro ...
- 【原创】 Shuffling
在机器学习领域中,经常会听到“shuffling"这个术语.那么,shuffling到底是什么意思呢. 通常,shuffling指的是在SGD怎样依赖训练数据输入顺序的算法中,将训练数据随机 ...
- 数据结构练习 00-自测5. Shuffling Machine (20)
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...
- 1042. Shuffling Machine (20) - sstream实现数字转字符串
题目例如以下: Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffli ...
- 自测-5 Shuffling Machine
Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...
- PAT 1042. Shuffling Machine (20)
1042. Shuffling Machine (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shu ...
- PAT1042:Shuffling Machine
1042. Shuffling Machine (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shu ...
随机推荐
- linux命令学习笔记(6):rmdir 命令
今天学习一下linux中命令: rmdir命令.rmdir是常用的命令,该命令的功能是删除空目录,一个目录 被删除之前必须是空的.(注意,rm - r dir命令可代替rmdir,但是有很大危险性.) ...
- Chrome focus样式
-webkit-box-shadow: 0 0 0 1000px #131313 inset;//获取焦点时候可以去除背景色 -webkit-text-fill-color: #fff;//字体颜色
- SPOJ705 Distinct Substrings (后缀自动机&后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- tiny4412sdk-1506原生uboot卡死
于16年2月多购买了tiny4412sdk-1506,用友善之臂(以下简称友善)的superboot是可以进入linux,而用三星原始的uboot_tiny4412-20130729则不可以.出现现象 ...
- rman命令详解(三)
1. Report 命令用户判断数据库的当前可恢复状态和提供数据库备份的特定信息1.1 指定最近没有备份的数据文件查询3天内没有备份过的表空间,可以用如下命令:RMAN> report need ...
- python构造一个http请求
我们经常会用python来进行抓包,模拟登陆等等, 势必要构造http请求包. http的request通常有4个方法get,post,put,delete,分别对应于查询,更新,添加,删除.我们经常 ...
- Poj 1936,3302 Subsequence(LCS)
一.Description(3302) Given a string s of length n, a subsequence of it, is defined as another string ...
- Poj1207 The 3n + 1 problem(水题(数据)+陷阱)
一.Description Problems in Computer Science are often classified as belonging to a certain class of p ...
- POJ2513(字典树+图的连通性判断)
//用map映射TLE,字典树就AC了#include"cstdio" #include"set" using namespace std; ; ;//26个小 ...
- Mybatis连接mysql数据库出现乱码
对于mysql数据库的乱码问题,有两中情况: 1. mysql数据库编码问题(建库时设定). 2. 连接mysql数据库的url编码设置问题. 对于第一个问题,目前个人发现只能通过重新建库解决,建库的 ...