UVALive - 6952 DP 分段/隔板
题意:商品总价按四舍五入计算,n个物品最多可分\(d+1\)段,求最小代价
\(dp[i][j]\):\(j\)个物品分\(i\)段
注意一个技巧是只在需要分出新的段时才四舍五入(旧段结算),这样就避免了不知道分段具体位置无法\(dp\)的情况
数据量比较小就不使用滚动数组了
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#define rep(i,j,k) for(register int i=j;i<=k;i++)
#define rrep(i,j,k) for(register int i=j;i>=k;i--)
#define erep(i,u) for(register int i=head[u];~i;i=nxt[i])
#define iin(a) scanf("%d",&a)
#define lin(a) scanf("%lld",&a)
#define din(a) scanf("%lf",&a)
#define s0(a) scanf("%s",a)
#define s1(a) scanf("%s",a+1)
#define print(a) printf("%lld",(ll)a)
#define enter putchar('\n')
#define blank putchar(' ')
#define println(a) printf("%lld\n",(ll)a)
#define IOS ios::sync_with_stdio(0)
using namespace std;
const int maxn = 1e5+11;
const int oo = 0x3f3f3f3f;
const double eps = 1e-7;
typedef long long ll;
ll read(){
ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
ll dp[22][maxn],p[maxn];
int n,d;
inline ll cal(ll x){
return (x+5)/10*10;
}
int main(){
while(cin>>n>>d){
memset(dp,0x3f,sizeof dp);
rep(i,1,n) p[i]=read();
dp[1][1]=p[1];
rep(i,2,n) dp[1][i]=dp[1][i-1]+p[i];
rep(i,2,d+1){
rep(j,i,n){
dp[i][j]=min(cal(dp[i-1][j-1])+p[j],dp[i][j-1]+p[j]);
}
}
ll ans=oo;
rep(i,1,d+1) ans=min(ans,cal(dp[i][n]));
println(ans);
}
return 0;
}
UVALive - 6952 DP 分段/隔板的更多相关文章
- UVALive - 6952 Cent Savings dp
题目链接: http://acm.hust.edu.cn/vjudge/problem/116998 Cent Savings Time Limit: 3000MS 问题描述 To host a re ...
- Codeforces Round #207 (Div. 1) D - Bags and Coins 构造 + bitset优化dp + 分段查找优化空间
D - Bags and Coins 思路:我们可以这样构造,最大的那个肯定是作为以一个树根,所以我们只要找到一个序列a1 + a2 + a3 .... + ak 并且ak为 所有点中最大的那个,那么 ...
- UVAlive 6131 dp+斜率优化
这道题和06年论文<从一类单调性问题看算法的优化>第一道例题很相似. 题意:给出n个矿的重量和位置,这些矿石只能从上往下运送,现在要在这些地方建造m个heap,要使得,sigma距离*重量 ...
- UVALive 4764 dp
DES: 这是一个新的游戏.给你一套牌.编号从1到100000.正常来说.你手中的牌和这次翻的牌是一样的,就会加一分.但是.如果是999的话.加三分.所以问你最大的分是多少. 貌似是简单的DP吧.(D ...
- UVALive - 4867 —— dp
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 4731 dp+贪心
这个题首先要利用题目的特性,先贪心,否则无法进行DP 因为求期望的话,越后面的乘的越大,所以为了得到最小值,应该把概率值降序排序,把大的数跟小的系数相乘 然后这种dp的特性就是转移的时候,由 i推到i ...
- hdu3480 Division(dp平行四边形优化)
题意:将n个数分成m段,每段的代价为最大值减最小值的平方,为代价最小是多少n<=10000 ,m<=5000 题解:先拍好序,从小到大,这样绝对是花费最小的,不过怎么样来做呢?一定很容易想 ...
- POJ 3486 & HDU 1913 Computers(dp)
题目链接:PKU:HDU: PKU:http://poj.org/problem?id=3486 HDU:pid=1913" target="_blank">htt ...
- 大白第一章第四节dp例题
入口 UVALive - 3882 #include<cstdio> using namespace std; ; int n,m,k,f[N]; int main(){ //f[i]表示 ...
随机推荐
- 面试题:servlet jsp cook session 背1
一.Servlet是什么?JSP是什么?它们的联系与区别是什么? Servlet是Java编写的运行在Servlet容器的服务端程序,狭义的Servlet是指Servlet接口,广义的Servlet是 ...
- [GO]随机数的使用
package main import ( "math/rand" "time" "fmt" ) func main() { //设置种子, ...
- Html.Partial方法和Html.RenderPartial方法
分布视图 PartialView 一般是功能相对独立的,类似用户控件的视图代码片段,可以被多个视图引用,引用方式如下. 1,Html.Partial方法和Html.RenderPartial方法 静态 ...
- HttpAnalyzerStdV7安装教程
相关链接:HttpAnalyzerStdV7使用教程 安装步骤: 1.解压压缩包 2.双击运行安装文件 3.根据向导提示点击Next 4.选择接受协议,点击Next 5.修改安装路 ...
- HTML5移动应用开发入门经典 中文pdf扫描版
HTML5是关注度ZUI高的前沿Web技术,而移动互联网则是近两年ZUI炙手可热的Web领域.<HTML5移动应用开发入门经典>将这两者巧妙结合起来,详细讲解了如何利用HTML5进行移动应 ...
- java8之流的基本使用(二)
概述 流(stream())是java8的一个新特性,主要的作用就是将各种类型的集合转换为流,然后的方便迭代数据用的.例如: //将List类型的集合转换为流 list.stream() 转换为流之后 ...
- NSURLConnection 网络请求
前言 DEPRECATED: The NSURLConnection class should no longer be used. NSURLSession is the replacement f ...
- 协程《三》gevent模块
一 gevent模块 安装 pip3 install gevent Gevent 是一个第三方库,可以轻松通过gevent实现并发同步或异步编程,在gevent中用到的主要模式是Greenlet, 它 ...
- 多线程《四》Thread对象的其他属性和方法
Thread对象的其他属性或方法 介绍 Thread实例对象的方法 # isAlive(): 返回线程是否活动的. # getName(): 返回线程名. # setName(): 设置线程名. th ...
- rtabmap and rtabmap_ros make error(rtabmap编译错误)
Build from source following README.nd in rtabmap_ros rtabmap make error Error 1 make[2]: *** No rule ...