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-----------------------------------------------------------------------
UVALive - 6872 Restaurant Ratings 数位dp的更多相关文章
- UVALive 4877 Non-Decreasing Digits 数位DP
4877 Non-Decreasing Digits A number is said to be made up ofnon-decreasing digitsif all the digits t ...
- UVALive - 6575 Odd and Even Zeroes 数位dp+找规律
题目链接: http://acm.hust.edu.cn/vjudge/problem/48419 Odd and Even Zeroes Time Limit: 3000MS 问题描述 In mat ...
- CSUOJ 1635 Restaurant Ratings
1635: Restaurant Ratings Time Limit: 1 Sec Memory Limit: 128 MB Description A famous travel web sit ...
- 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP
[BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...
- bzoj1026数位dp
基础的数位dp 但是ce了一发,(abs难道不是cmath里的吗?改成bits/stdc++.h就过了) #include <bits/stdc++.h> using namespace ...
- uva12063数位dp
辣鸡军训毁我青春!!! 因为在军训,导致很长时间都只能看书yy题目,而不能溜到机房鏼题 于是在猫大的帮助下我发现这道习题是数位dp 然后想起之前讲dp的时候一直在补作业所以没怎么写,然后就试了试 果然 ...
- HDU2089 不要62[数位DP]
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 数位DP GYM 100827 E Hill Number
题目链接 题意:判断小于n的数字中,数位从高到低成上升再下降的趋势的数字的个数 分析:简单的数位DP,保存前一位的数字,注意临界点的处理,都是套路. #include <bits/stdc++. ...
- 数位dp总结
由简单到稍微难点. 从网上搜了10到数位dp的题目,有几道还是很难想到的,前几道基本都是模板题,供入门用. 点开即可看题解. hdu3555 Bomb hdu3652 B-number hdu2089 ...
随机推荐
- 从tomcat下载文件的配置方法(很全呢)
前几天我做的项目有个下载文件的东西让我苦恼了一下,上传的文件没有放到OSS服务器,而是直接放到tomcat内, 我就想做一个a标签直接下载的得了,结果点开一直都说没有该文件,我查了很多资料找到了如何配 ...
- redis集群部署步骤
1.yum 安装依赖 yum install gcc unzip wget 2.编译安装redis,编译安装的目的是源码包内包含了接下来创建redis集群所需要的 redis-trib.rb脚本 ma ...
- 【NXP开发板应用—智能插排】3.驱动GPIO点亮外接LED
[前言] 首先感谢深圳市米尔科技有限公司举办的这次活动并予以本人参加这次活动的机会,以往接触过嵌入式,但那都是皮毛,最多刷个系统之类的,可以说对于嵌入式系统开发这件事情是相当非常陌生的,这次活动为我提 ...
- STM32JTAG口用作普通IO的配置
使用Jlink向STM32烧录程序时,需要使用6个芯片的引脚(以STM32F103C8T6为例),分别是PB4/JNTRST.PB3/JTDO.PA13/JTMS.PA14/JTCK.PA15/JTD ...
- Python2和Python3
1. 字符编码 1.1. Python2默认为ACSII编码 1.2. Python3为Unicode 2. Unicode和UTF8和GBK编码的关系 utf8:中文3字节 ...
- 使用bison和yacc制作脚本语言(4)
我们现在开始设计数据结构: interpreter.h #ifndef INTERPRETER #define INTERPRETER #include "../include/eval.h ...
- [原创]利用python构造ICMP Echo Request并发送
import socket import struct def checksum(source_string): sum = 0 countTo = (len(source_string)/2)*2 ...
- Redis系列七 主从复制(Master/Slave)
主从复制(Master/Slave) 1.是什么 也就是我们所说的主从复制,主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制,Master以写为主,Slave以读为主. 2 ...
- 使数据可供ArcGIS Server访问
内容来自ESRI官方文档(点击访问),简单总结如下: 1 ArcGIS Server用于发布服务的数据必须存储在服务器可以访问的位置: 2 这样的位置有三种类型: 本地路径:将数据本地存储在每台 Ar ...
- JQuery.extend扩展实现同步post请求
有时需要在jQuery中实现同步post请求,而jquery自带的是异步,需要通过JQuery.extend扩展. 支持ie和firefox,方法转载而来.需要在submit前将form.append ...