hdu5542 数据结构优化dp

树状数组+dp


Problem Description
Cao Cao made up a big army and was going to invade the whole South China. Yu Zhou was worried about it. He thought the only way to beat Cao Cao is to have a spy in Cao Cao’s army. But all generals and soldiers of Cao Cao were loyal, it’s impossible to convince any of them to betray Cao Cao.

So there is only one way left for Yu Zhou, send someone to fake surrender Cao Cao. Gai Huang was selected for this important mission. However, Cao Cao was not easy to believe others, so Gai Huang must leak some important information to Cao Cao before surrendering.

Yu Zhou discussed with Gai Huang and worked out N information to be leaked, in happening order. Each of the information was estimated to has ai value in Cao Cao’s opinion.

Actually, if you leak information with strict increasing value could accelerate making Cao Cao believe you. So Gai Huang decided to leak exact M information with strict increasing value in happening order. In other words, Gai Huang will not change the order of the N information and just select M of them. Find out how many ways Gai Huang could do this.

Input
The first line of the input gives the number of test cases, T(1≤100). T test cases follow.

Each test case begins with two numbers N(1≤N≤103) and M(1≤M≤N), indicating the number of information and number of information Gai Huang will select. Then N numbers in a line, the ith number ai(1≤ai≤109) indicates the value in Cao Cao’s opinion of the ith information in happening order.

Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the ways Gai Huang can select the information.

The result is too large, and you need to output the result mod by 1000000007(109+7).

Sample Input
2
3 2
1 2 3
3 2
3 2 1

Sample Output
Case #1: 3
Case #2: 0
Hint

In the first cases, Gai Huang need to leak 2 information out of 3. He could leak any 2 information as all the information value are in increasing order.
In the second cases, Gai Huang has no choice as selecting any 2 information is not in increasing order.

题意:给定一个序列,问你有多少个长度为m的递增子序列。
思路:定义dp[i][j],i是长度为i,j是以j为结尾,这样的子序列有多少个,利用三层循环可以很容易的求得,但是,显然这样会超时,需要优化.
所以我们可以用树状数组维护,原数组离散化到2----n+1区间上同时创建一个虚拟节点1,用于第一次转移,对于每次循环i,需要重现建立树状数组.具体实现见代码
简单的限制条件用循环处理,复杂的限制条件用数据结构维护
另外这道题一开始想用线段树,不知道为什么 t了,大概是我太菜,优化不够

#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
typedef long long ll;
struct node1{
   
	ll sum,l,r;
}t[8000];
struct node2{
   
	ll a,b,c;
}a[1005];
ll n,m;
ll f[1005][1005],res=0;
ll c[2000];
ll ask(ll x){
   
	ll ans=0;
	for(;x;x-=x&-x)	ans=(ans+c[x])%mod;
	return ans;
}
void add(ll x,ll y){
   
	for(;x<=n+1;x+=x&-x)	c[x]=(c[x]+y)%mod;
}
bool cmp(node2 a,node2 b){
   
	return a.a<b.a;
}
bool cmp1(node2 a,node2 b){
   
	return a.b<b.b;
}
/*void build(ll l,ll r,ll k){ t[k].l=l; t[k].r=r; t[k].sum=0; if(t[k].l==t[k].r) return; int mid=(l+r)/2; build(l,mid,k*2); build(mid+1,r,k*2+1); } void add(ll p,ll q,ll k){ if(t[k].l==t[k].r){ t[k].sum=(t[k].sum+q)%mod; return ; } int mid=(t[k].l+t[k].r)/2; if(p<=mid) add(p,q,k*2); else add(p,q,k*2+1); t[k].sum=(t[k*2].sum+t[k*2+1].sum)%mod; } void ask(int l,int r,int k){ //printf("a%d %d %d %d %d\n",t[k].l,t[k].r,k,l,r); if(l<=t[k].l&&t[k].r<=r){ res=(res+t[k].sum)%mod; return; } int mid=(t[k].l+t[k].r)/2; if(l<=mid) ask(l,r,k*2); if(r>mid) ask(l,r,k*2+1); }*/
int main(){
   
	int t,ttt=1;
	scanf("%lld",&t);
	while(t--){
   
		memset(f,0,sizeof(f));
		printf("Case #%d: ",ttt++);
		scanf("%lld%lld",&n,&m);
		for(int i=1;i<=n;i++){
   
			scanf("%lld",&a[i].a);
			a[i].b=i;
		}
		sort(a+1,a+n+1,cmp);
		for(int i=1;i<=n+1;i++)
			a[i].c=i+1;
		sort(a+1,a+n+1,cmp1);
		a[0].a=-inf;	a[0].b=0;	a[0].c=1;
		f[0][0]=1;
		for(int i=1;i<=m;i++){
   
			memset(c,0,sizeof(c));
			add(1,f[i-1][0]);
			for(int j=1;j<=n;j++){
   
				res=ask(a[j].c-1);
				f[i][j]=res;
				add(a[j].c,f[i-1][j]);
			}
		}
		ll maxx=0;
		for(int i=1;i<=n;i++)
			maxx=(maxx+f[m][i])%mod;
		printf("%lld\n",maxx%mod);
	}
	return 0;
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务