HDU 4258(Covered Walkway-斜率优化)
Covered Walkway
Time Limit: 30000/10000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1273 Accepted Submission(s): 491
The building contractor has an interesting pricing scheme. To cover the walkway from a point atx to a point at
y, they will charge c+(x-y)2, wherec is a constant. Note that it is possible for
x=y. If so, then the contractor would simply chargec.
Given the points along the walkway and the constant c, what is the minimum cost to cover the walkway?
c (1≤c≤109), wheren is the number of points which must be covered, and
c is the contractor’s constant. Each of the followingn lines will contain a single integer, representing a point along the walkway that must be covered. The points will be in order, from smallest to largest. All of the points
will be in the range from 1 to 109, inclusive. The input will end with a line with two 0s.
yield answers which will fit in a signed 64-bit integer.
10 5000
1
23
45
67
101
124
560
789
990
1019
0 0
30726
pid=4257">4257
4260pid=4261">4261
4262设f[i]表示仅仅考虑前n个的最小费用
显然f[i]=f[j-1]+c+sqr(a[i]-a[j])
若选i比选j优(i<j)
f[i-1]+sqr(a[i]-a[y])+c<f[j-1]+sqr(a[j]-a[y])+c
f[i-1]+sqr(a[i])-f[j-1]-sqr(a[j])
----------------------------------- > 2*a[y]
a[i]-a[j]
斜率优化。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000000+10)
#define eps 1e-13
#define Read(x) { \
while (!isdigit(c=getchar())); \
x=c-48; \
while (isdigit(c=getchar())) x=x*10+c-48; \
}
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n;
ll cost;
char c;
ll a[MAXN],f[MAXN];
struct P
{
int i;
long double x,y;
P(int _i,ll _x,ll _y):i(_i),x(_x),y(_y){}
P(ll _x,ll _y):x(_x),y(_y){}
P(){}
friend long double kk(P a,P b){if (abs(a.x-b.x)<eps) return (b.y-a.y)*INF;return (b.y-a.y)/(b.x-a.x); }
}st[MAXN];
struct V
{
long double x,y;
V(ll _x,ll _y):x(_x),y(_y){}
V(){}
V(P a,P b):x(b.x-a.x),y(b.y-a.y){}
friend long double operator*(V a,V b){return a.x*b.y-a.y*b.x; }
};
int main()
{
freopen("B.in","r",stdin); while (1)
{
scanf("%d%lld",&n,&cost);
if (!n) break;
For(i,n)
Read(a[i]) f[0]=0;
int head=1,tail=1;
st[1]=P(1,a[1],a[1]*a[1]);
Fork(i,1,n)
{
P A=P(i,a[i],f[i-1]+a[i]*a[i]);
while (head^tail&&V(st[tail-1],st[tail])*V(st[tail],A)<=0) tail--;
st[++tail]=A; while (head^tail&&kk(st[head],st[head+1])<2*a[i]) head++;
int k=st[head].i;
f[i]=f[k-1]+cost+(a[k]-a[i])*(a[k]-a[i]);
}
cout<<f[n]<<endl; }
return 0;
}
HDU 4258(Covered Walkway-斜率优化)的更多相关文章
- HDU 4258 Covered Walkway 斜率优化DP
Covered Walkway Problem Description Your university wants to build a new walkway, and they want at ...
- hdu 4258 Covered Walkway
题目大意: 一个N个点的序列,要将他们全部覆盖,求总最少费用:费用计算: c+(x-y)2 分析: 斜率优化DP 我们假设k<j<i.如果在j的时候决策要比在k的时候决策好,那么也是就是d ...
- HDU 3507 单调队列 斜率优化
斜率优化的模板题 给出n个数以及M,你可以将这些数划分成几个区间,每个区间的值是里面数的和的平方+M,问所有区间值总和最小是多少. 如果不考虑平方,那么我们显然可以使用队列维护单调性,优化DP的线性方 ...
- hdu 3507 Print Article(斜率优化DP)
题目链接:hdu 3507 Print Article 题意: 每个字有一个值,现在让你分成k段打印,每段打印需要消耗的值用那个公式计算,现在让你求最小值 题解: 设dp[i]表示前i个字符需要消耗的 ...
- HDU 2829 Lawrence(斜率优化DP O(n^2))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...
- HDU 3507 Print Article 斜率优化
Print Article Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- HDU 3480 Division(斜率优化+二维DP)
Division Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 999999/400000 K (Java/Others) Tota ...
- HDU.2829.Lawrence(DP 斜率优化)
题目链接 \(Description\) 给定一个\(n\)个数的序列,最多将序列分为\(m+1\)段,每段的价值是这段中所有数两两相乘的和.求最小总价值. \(Solution\) 写到这突然懒得写 ...
- HDU 2829 [Lawrence] DP斜率优化
解题思路 首先肯定是考虑如何快速求出一段铁路的价值. \[ \sum_{i=1}^k \sum_{j=1, j\neq i}^kA[i]A[j]=(\sum_{i=1}^kA[i])^2-\sum_{ ...
随机推荐
- [Swift通天遁地]四、网络和线程-(1)线程的锁和解锁
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- MyBatis Generator实现MySQL分页插件
MyBatis Generator是一个非常方便的代码生成工具,它能够根据表结构生成CRUD代码,可以满足大部分需求.但是唯一让人不爽的是,生成的代码中的数据库查询没有分页功能.本文介绍如何让MyBa ...
- 绑定树tree 的后台方法
#region 获取部门列表树集合 /// <summary> /// 获取部门列表树集合 /// </summary> ...
- 传值:web.xml传递参数 即在Servlet中获取web.xml里的值
传值:web.xml传递参数 在web.xml中的Servlet里配置多个init-param <servlet> ... <init-param> <param-nam ...
- 为什么使用HttpServlet?http协议特点、servlet
因为只有HttpServlet是基于http协议,实现Servlet接口,而http协议是短连接协议,能够实现客户端访问服务端后,数据交互后 连接自动断开.同时http协议基于tcp.ip协议,封装了 ...
- C#模拟百度登录并到指定网站评论回帖(二)
序言: 回归正题:前面讲到的抓包分析的数据,是模拟登录要获得得必要信息(当然有些也不是必要的...我只是都列举出来这样有个对比)如果说,有哪个英文字母不知道什么意思的,可以问一下度娘,有不少前辈都发过 ...
- Spring Cloud (14) 服务网关-过滤器
Spring Cloud Zuul作为网关所具备的最基本的功能:路由,还具备另外一个核心的功能:过滤器. 过滤器 通过Spring Cloud Zuul实现的路由功能,我们的微服务可以通过统一的API ...
- NHibernate系列学习(一)-看看用NH怎么做增速改查
1.本次所有代码是在一下环境下运行的 学习系统:win8 64bit 开发环境:VS2013,MSSQL2012 NHibernate版本:NHibernate-4.0.3.GA [文章结尾有本次笔记 ...
- Echarts 出现不明竖线解决方案
Echarts出现了不明竖线,百思不得其解.去查相应的解决方案也没有找到. 后来自己点来点去,突然感觉像是上一个Echarts遗留的. 然后去Echarts官网看到了 clear()方法,这个方法可以 ...
- .net 大数据量,查找Where优化(List的Contains与Dictionary的ContainsKey的比较)
最近优化一个where查询条件,查询时间很慢,改为用Dictionary就很快了. 一.样例 假设:listPicsTemp 有100w条数据,pictures有1000w条数据. 使用第1段代码执 ...