UVALive - 6872 Restaurant Ratings 数位dp

题目链接:

http://acm.hust.edu.cn/vjudge/problem/113727

Restaurant Ratings

Time Limit: 3000MS

题意

给你一个长度为n,由非负整数组成的和为sum的序列。求长度为也为n,且和比sum小的或者和等于sum并且字典序小于所给序列的所有不同的序列。

题解

数位dp。
dp[i][j]表示序列长度为i时和为j的所有不同序列数。 sumv[i][j]表示长度为i并且和小于等于j的所有不同序列数。

代码

#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)

typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;

const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;

//start----------------------------------------------------------------------

LL dp[22][33],sumv[22][33];

void pre(){
	clr(dp,0);
	dp[0][0]=1;
	rep(i,1,22){
		rep(j,0,33){
			rep(k,0,j+1){
				dp[i][j]+=dp[i-1][j-k];
			}
			if(j==0) sumv[i][j]=dp[i][j];
			else sumv[i][j]=sumv[i][j-1]+dp[i][j];
		}
	}
}

int arr[22];

int main() {
	pre();
    int n;
    while(scanf("%d",&n)==1&&n){
    	int sum[22];
    	sum[0]=0;
    	rep(i,1,n+1){
    		scanf("%d",&arr[i]);
    		sum[i]=sum[i-1]+arr[i];
		}
		LL ans=0;
    	if(sum[n]-1>=0) ans=sumv[n][sum[n]-1];
    	rep(i,1,n+1){
    		rep(j,0,arr[i]){
    			ans+=dp[n-i][sum[n]-sum[i-1]-j];
			}
		}
		ans++;
		printf("%lld\n",ans);
	} 
    return 0;
}

//end-----------------------------------------------------------------------
posted @ 2016-08-13 21:37  fenicnn  阅读(121)  评论(0编辑  收藏  举报