numbers

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 196608/196608 K (Java/Others)
Total Submission(s): 156    Accepted Submission(s): 50
Problem Description
 
Now you have a stack and n numbers 1,2,3,…,n.
These n numbers are pushed in the order and popped if the number is at the top of the stack.
You can read the sample to get more details.
This question is quite easy. Therefore I must give you some limits.
There are m limits, each is expressed as a pair<A,B> means the number A must be popped before B.
Could you tell me the number of ways that are legal in these limits?
I know the answer may be so large, so you can just tell me the answer mod 1000000007({10}^{9}+7).
 

Input
The first line contains an integer T(about 5),indicating the number of cases.
Each test case begins with two integers n(1 \leq n \leq 300) and m(1 \leq m \leq 90000).
Next m lines contains two integers A and B(1 \leq A \leq n,1 \leq B \leq n)
(P.S. there may be the same limits or contradict limits.)
 

Output
For each case, output an integer means the answer mod 1000000007.
 

Sample Input
5
1 0
5 0
3 2
1 2
2 3
3 2
2 1
2 3
3 3
1 2
2 3
3 1
 

Sample Output
1
42
1
2
0
 
Hint

The only legal pop-sequence of case 3 is 1,2,3. The legal pop-sequences of case 4 are 2,3,1 and 2,1,3.

 

Source

设f[i][j]为数字(i-j)的出栈方案数,显然我们可以枚举最后一个出栈的元素k,i<=k<=j。先把[i,k-1]出栈完了之后,把k放进去,然后再放[k+1,j]
并且出栈后再出k。
所以我们可以得到递推式 f[i][j]=Σf[i][k-1]*f[k+1][j] ,当然因为有限制这里的k显然不能取区间[i,j]的所有数。 当然A,B只能对 L<=min(A,B),R>=max(A,B)的f[L][R]产生影响。
我们发现当A<B时,只要k!=A就是合法的,因为出栈顺序是[i,k-1]->[k+1,j]->k;
而当A>B时,观察出栈顺序也可以发现只要k¢(B,A]就是合法的。 于是现在的问题变成了如何快速判断一个k在对于f[i][j]是否合法。。。。
我们可以发现的是,当把区间的左端点看成横坐标,区间的右端点看成纵坐标的时候,对于一组A、B
不合法的区域就是以(1,max(A,B))为左下角,(min(A,B),n)为右上角的矩形。
虽然这个坐标系只有在直线y=x上方的区域是有用的,但是我们不妨对每个k都做一遍差分,
然后再前缀和一下得到它在哪些区间是不合法的。
这样就可以 O(N^3 + N*M) 完成本题了。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int ha=1000000007;
int f[305][305],n,m,T;
int uu,vv,ban[305][305][305];
bool flag; inline int add(int x,int y){
x+=y;
return x>=ha?x-ha:x;
} inline void init(){
memset(f,0,sizeof(f));
memset(ban,0,sizeof(ban));
flag=0;
} inline void matrix(int px1,int py1,int px2,int py2,int num){
ban[num][px1][py1]++;
ban[num][px2+1][py1]--;
ban[num][px1][py2+1]--;
ban[num][px2+1][py2+1]++;
} inline void dp(){
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++) ban[i][j][k]+=ban[i][j-1][k]+ban[i][j][k-1]-ban[i][j-1][k-1]; for(int i=1;i<=n;i++) f[i][i]=f[i][i-1]=1;
f[n+1][n]=1; for(int len=1;len<n;len++)
for(int i=1,j;(j=i+len)<=n;i++)
for(int k=i;k<=j;k++) if(!ban[k][i][j]){
f[i][j]=add(f[i][j],f[i][k-1]*(ll)f[k+1][j]%ha);
}
} int main(){
scanf("%d",&T);
while(T--){
init();
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d",&uu,&vv);
if(uu<vv){
matrix(1,vv,uu,n,uu);
}
else if(uu>vv){
for(int j=vv+1;j<=uu;j++) matrix(1,uu,vv,n,j);
}
else flag=1;
} if(flag){
puts("0");
continue;
} dp(); printf("%d\n",f[1][n]);
} return 0;
}

  

 

Hdoj 5181 numbers的更多相关文章

  1. hdu 5181 numbers

    http://acm.hdu.edu.cn/showproblem.php?pid=5181 题意: 有一个栈,其中有n个数1~n按顺序依次进入栈顶,在某个时刻弹出. 其中m个限制,形如数字A必须在数 ...

  2. hdu 5181 numbers——思路+区间DP

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5181 题解:https://www.cnblogs.com/Miracevin/p/10960717.ht ...

  3. hdoj 5522 Numbers

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5522 水题:暴力过 #include<stdio.h> #include<strin ...

  4. HDU.5181.numbers(DP)

    题目链接 参考. \(Description\) 将\(1,2,\cdots,n(n\leq 300)\)依次入栈/出栈,并满足\(m(m\leq 90000)\)个形如\(x\)要在\(y\)之前出 ...

  5. 找规律/数位DP HDOJ 4722 Good Numbers

    题目传送门 /* 找规律/数位DP:我做的时候差一点做出来了,只是不知道最后的 is_one () http://www.cnblogs.com/crazyapple/p/3315436.html 数 ...

  6. HDOJ(HDU).1058 Humble Numbers (DP)

    HDOJ(HDU).1058 Humble Numbers (DP) 点我挑战题目 题意分析 水 代码总览 /* Title:HDOJ.1058 Author:pengwill Date:2017-2 ...

  7. hdoj 2817 A sequence of numbers【快速幂】

    A sequence of numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

    Problem Description Give you a lot of positive integers, just to find out how many prime numbers the ...

  9. HDOJ 1058 Humble Numbers(打表过)

    Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...

随机推荐

  1. 校内考试之zay与银临(day1)

    T1大美江湖(洛谷P5006) zayの题解: 这个题的本质是模拟 不过有卡ceil的地方 ceil是对一个double进行向上取整,而对于int/int来说,返回值是int 举个生动的栗子 ceil ...

  2. iview框架 两侧弹框 出现第二层弹框 一闪而过的问题

    分析原因:寡人怀疑可能是,两层弹出框 采用的是一个开关值,发生了覆盖 解决方式 是在第二层弹框外套层计时器 源代码如下: 修改后为:

  3. java的类加载器体系结构和双亲委派机制

    类加载器将字节码文件加载到内存中,同时在方法区中生成对应的java.land.class对象  作为外部访问方法区的入口. 类加载器的层次结构: 引导类加载器<-------------扩展类加 ...

  4. laravel5.2总结--文件上传

    1 配置 文件系统的配置文件在 config/filesystems.php 文件中,此处我们新建一个uploads本地磁盘空间用于存储上传的文件,具体配置项及说明如下: <?php retur ...

  5. linux学习(一) -- ubuntu下lamp环境的配置

    以下为实测教程,希望能为大家提供帮助,转载请注明出处 ubuntu+apache+mysql+php7 第一.更换apt的源 1.复制原文件备份 sudo cp /etc/apt/source.lis ...

  6. C#定时器,定时做什么事情

    http://www.cnblogs.com/bobositlife/archive/2015/09/29/aspnet-mvc-csharp-quartz-net-timer-task-schedu ...

  7. IOS开发---菜鸟学习之路--(十二)-利用ASIHTTPRequest进行异步获取数据

    想要实现异步获取的话我这边了解过来有两个非常简单的方式 一个是利用ASIHTTPRequest来实现异步获取数据 另一个则是利用MBProgressHUD来实现异步获取数据 本章就先来讲解如何利用AS ...

  8. PostgreSQL 数组类型

    PostgreSQL 支持表的字段使用定长或可变长度的一维或多维数组,数组的类型可以是任何数据库内建的类型.用户自定义的类型.枚举类型, 以及组合类型.但目前还不支持 domain 类型. 数组类型的 ...

  9. PAT——乙级1026and1046

    1026 程序运行时间 (15 point(s)) 要获得一个 C 语言程序的运行时间,常用的方法是调用头文件 time.h,其中提供了 clock() 函数,可以捕捉从程序开始运行到 clock() ...

  10. s debug

    value stack contents   ognl 值栈 stack context           action上下文 action上下文是一个map对象,通过#key获得对象内容,在#re ...