题目链接:

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

Cent Savings

Time Limit: 3000MS
#### 问题描述
> To host a regional contest like NWERC a lot of
> preparation is necessary: organizing rooms and
> computers, making a good problem set, inviting
> contestants, designing T-shirts, booking hotel
> rooms and so on. I am responsible for going
> shopping in the supermarket.
> When I get to the cash register, I put all my
> n items on the conveyor belt and wait until all
> the other customers in the queue in front of me
> are served. While waiting, I realize that this
> supermarket recently started to round the total
> price of a purchase to the nearest multiple of 10
> cents (with 5 cents being rounded upwards). For
> example, 94 cents are rounded to 90 cents, while
> 95 are rounded to 100.
> It is possible to divide my purchase into groups and to pay for the parts separately. I managed to
> find d dividers to divide my purchase in up to d + 1 groups. I wonder where to place the dividers to
> minimize the total cost of my purchase. As I am running out of time, I do not want to rearrange items
> on the belt.
#### 输入
> The input file contains several test cases, each of them as described below.
> The input consists of:
> • one line with two integers n (1 ≤ n ≤ 2000) and d (1 ≤ d ≤ 20), the number of items and the
> number of available dividers;
> • one line with n integers p1, . . . , pn (1 ≤ pi ≤ 10000 for 1 ≤ i ≤ n), the prices of the items in
> cents. The prices are given in the same order as the items appear on the belt.
#### 输出
> For each test case, output the minimum amount of money needed to buy all the items, using up to d
> dividers.

样例

sample input

5 1

13 21 55 60 42

5 2

1 1 1 1 1

sample output

190

0

题意

给你一个数组,现在要把它最多切d刀分成d+1块,对每一块的和要四舍五入,问如果分割使得最后的和最大。

题解

各种姿势的dp:

dp[i][j][k]表示前i个,切了j刀,最后一刀切完之后的值%10为k的舍入的最大偏差。

则最后的结果=min(sum-dp[n][j][k])。

#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; const int maxn=2222; int n,d; int arr[maxn];
int dp[maxn][22][11]; inline int f(int x){
if(x<5) return x;
else return x-10;
} int main() {
while(scanf("%d%d",&n,&d)==2){
rep(i,0,maxn) rep(j,0,22) rep(k,0,11) dp[i][j][k]=-INF;
dp[0][0][0]=0;
int sum=0;
rep(i,1,n+1){
scanf("%d",&arr[i]);
sum+=arr[i];
}
rep(i,0,n){
rep(j,0,d+1){
rep(k,0,10){
int nk=(arr[i+1]+k)%10;
dp[i+1][j][nk]=max(dp[i+1][j][nk],dp[i][j][k]-f(k)+f(nk)); nk=arr[i+1]%10;
if(j+1<=d) dp[i+1][j+1][nk]=max(dp[i+1][j+1][nk],dp[i][j][k]+f(nk));
}
}
}
int ma=f(sum%10);
rep(j,0,d+10){
rep(k,0,10){
ma=max(ma,dp[n][j][k]);
}
}
printf("%d\n",sum-ma);
}
return 0;
}

dp[i][j]表示前i个切了j刀得到的最小和(刀切在i后面或不切)。

#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 pb(v) push_back(v)
#define sz() size()
#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---------------------------------------------------------------------- const int maxn=2222;
const int maxm=22;
int n,d;
int dp[maxn][maxm];
int arr[maxn]; int f(int x){
int t=x%10;
if(t>=5) x+=10;
x-=t;
return x;
} int main() {
while(scanf("%d%d",&n,&d)==2){
clr(dp,0x3f);
dp[0][0]=0;
rep(i,1,n+1) scanf("%d",&arr[i]);
rep(i,1,n){
rep(j,0,d+1){
if(j==0) dp[i][j]=dp[i-1][j]+arr[i];
else dp[i][j]=min(dp[i-1][j]+arr[i],f(dp[i-1][j-1]+arr[i]));
}
}
int ans=INF;
rep(j,0,d+1) ans=min(ans,f(dp[n-1][j]+arr[n]));
printf("%d\n",ans);
}
return 0;
} //end-----------------------------------------------------------------------

dp[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 pb(v) push_back(v)
#define sz() size()
#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; const int maxn=2222;
const int maxm=22;
int n,d;
int dp[maxn][maxm];
int arr[maxn],sumv[maxn]; int f(int x){
int t=x%10;
if(t>=5) x+=10;
x-=t;
return x;
} int main() {
while(scanf("%d%d",&n,&d)==2){
clr(dp,0x3f);
dp[0][0]=0; sumv[0]=0;
rep(i,1,n+1){
scanf("%d",&arr[i]);
sumv[i]=sumv[i-1]+arr[i];
dp[i][0]=f(sumv[i]);
}
rep(i,1,n+1){
rep(j,1,d+1){
rep(k,0,i){
dp[i][j]=min(dp[i][j],dp[k][j-1]+f(sumv[i]-sumv[k]));
}
}
}
int ans=f(sumv[n]);
rep(j,1,d+1) ans=min(ans,dp[n][j]);
printf("%d\n",ans);
}
return 0;
}

UVALive - 6952 Cent Savings dp的更多相关文章

  1. uva 6952 Cent Savings dp

    Cent Savings Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/vie ...

  2. UVALive - 6952 DP 分段/隔板

    题意:商品总价按四舍五入计算,n个物品最多可分\(d+1\)段,求最小代价 \(dp[i][j]\):\(j\)个物品分\(i\)段 注意一个技巧是只在需要分出新的段时才四舍五入(旧段结算),这样就避 ...

  3. UVALive - 6529 找规律+dp

    题目链接: http://acm.hust.edu.cn/vjudge/problem/47664 Eleven Time Limit: 5000MS 问题描述 In this problem, we ...

  4. UVaLive 6801 Sequence (计数DP)

    题意:给定一个序列,有 n 个数,只有01,然后你进行k次操作,把所有的1变成0,求有多种方法. 析:DP是很明显的,dp[i][j] 表示进行第 i 次操作,剩下 j 个1,然后操作就两种,把1变成 ...

  5. UVaLive 6697 Homework Evaluation (DP)

    题意:给出一个长字符串,再给一个短字符串,进行匹配,如果第i个恰好匹配,则 +8,:如果不匹配,可以给长或短字符串添加-,先后匹配,这样-3, 连续的长字符串添加-,需要减去一个4:也可不给添加-,则 ...

  6. UVaLive 7374 Racing Gems (DP,LIS)

    题意:以辆赛车可以从x轴上任意点出发,他的水平速度允许他向每向上移动v个单位,就能向左或向右移动v/r个单位(也就是它的辐射范围是个等腰三角形) 现在赛车从x轴出发,问它在到达终点前能吃到的最多钻石. ...

  7. UVALive 6947 Improvements(DP+树状数组)

    [题目链接] https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=sho ...

  8. UVaLive 3490 Generator (KMP + DP + Gauss)

    题意:随机字母组成一个串,有一个目标串,当这个由随机字母组成的串出现目标串就停止,求这个随机字母组成串的期望长度. 析:由于只要包含目标串就可以停止,所以可以先把这个串进行处理,也就是KMP,然后dp ...

  9. UVALive 6908 Electric Bike dp

    Electric Bike 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8 ...

随机推荐

  1. javascript中for/in循环及使用技巧

    JavaScript 支持不同类型的循环: for - 循环代码块一定的次数 for/in - 循环遍历对象的属性 while - 当指定的条件为 true 时循环指定的代码块 do/while - ...

  2. Ninject在mvc中的简单配置

    前言 Ninject是一款开源的轻量级的依赖注入插件.从接触ioc以来,一直都是使用这个,感觉用起来还是不错的,配置起来也很方便简单.在mvc中更是基本傻瓜式的配置. 开发前的准备 新建一个mvc3项 ...

  3. 合并多个List<T>类型并通过LINQ按指定属性排序

    后台CS代码: namespace WebFormTest.TestCollect { public partial class ListTest : System.Web.UI.Page { pro ...

  4. 学习c编程的第三天

    #include<stdio.h> int add(int,int); int main(){ int x=add(1,2); printf("%d",x); retu ...

  5. zookeeper典型应用场景之一:master选举

    对于zookeeper这种东西,仅仅知道怎么安装是远远不够的,至少要对其几个典型的应用场景进行了解,才能比较全面的知道zk究竟能干啥,怎么玩儿,以后的日子里才能知道这货如何能为我所用.于是,有了如下的 ...

  6. php根据身份证号码计算年龄

    代码如下 复制代码 <?php function getAgeByID($id){         //过了这年的生日才算多了1周岁         if(empty($id)) return ...

  7. 在Activity中设置new出来的TextView属性

    //创建一个TextView---->textView TextView textView = new TextView(this);   // 第一个参数为宽的设置,第二个参数为高的设置 te ...

  8. 【原】RDD专题

    RDD是什么东西?在Spark中有什么作用?如何使用?  1.RDD是什么 (1)为什么会产生RDD? 传统的MapReduce虽然具有自动容错.平衡负载和可拓展性的优点,但是其最大缺点是采用非循环式 ...

  9. U6会计科目导入致对账不平

    一个客户,用的是U6版本,想将己使用的账套的科目导入新建的账套中,本想用用友本身自带的总账工具导入,发现导不了.没办法,只能打开数据库,手工导入. 月末结账时,发现对账不平.问题是余额表与明细不平,大 ...

  10. 百度 UEditor 简单安装调试和调用,网上其它的教程太官方了,不适合新手

    对于新手来说,只要能实现功能即可,其它设置完全默认. 预览图: 1.首先 到官网下载,这个不多说.下载后解压到网站你想要的目录,我这里放到根目录下在你需要使用编辑器的地方,插入如下HTML代码: &l ...