UVALive - 6872 Restaurant Ratings 数位dp
2016-08-13 21:37
429 查看
题目链接:
http://acm.hust.edu.cn/vjudge/problem/113727Restaurant 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 -1>=0) ans=sumv [sum -1]; rep(i,1,n+1){ rep(j,0,arr[i]){ ans+=dp[n-i][sum -sum[i-1]-j]; } } ans++; printf("%lld\n",ans); } return 0; } //end-----------------------------------------------------------------------
相关文章推荐
- UVALive 3675 Sorted bit sequence(数位dp+二分)
- UVALive 5004 Balanced Number && hdu-3967 Zero's Number(数位dp)
- UVALive 4864 数位dp
- [UVALive4864] Bit Counting && 数位DP
- uvalive7271(A Math Problem) 数位dp
- UVALive 7271 A Math Problem 【数位dp计数】
- [UVALive3675]Sorted bit sequence && 数位DP
- UVALive 3782 Bigger is Better(数位DP + 大数)
- UVALive 4864 Bit Counting --记忆化搜索 / 数位DP?
- UVALive 2031 Dance Dance Revolution (舞步转移,状态压缩DP,4级)
- UVALive6257-剪枝|dp-Chemist's vows
- UVAlive 6697 - Homework Evaluation(DP)
- UVALive 4651(DP)
- uvalive5790(DP)
- UVALive 6462 状压DP
- UVALive 5088 Alice and Bob's Trip(树形DP)
- uva 10401 数位DP
- UVALive - 4256 || LA 4256 Salesmen 商人(DP)
- UVALive - 4614 Moving to Nuremberg (树形DP)
- uva 11361 - Investigating Div-Sum Property(数位dp)