BZOJ-1721|线性dp-缆车支柱
Ski Lift 缆车支柱
Description
Farmer Ron in Colorado is building a ski resort for his cows (though budget constraints dictate construction of just one ski lift). The lift will be constructed as a monorail and will connect a concrete support at the starting location to the support at the ending location via some number of intermediate supports, each of height 0 above its land. A straight-line segment of steel connects every pair of adjacent supports. For obvious reasons, each section of straight steel must lie above the ground at all points. Always frugal, FR wants to minimize the number of supports that he must build. He has surveyed the N (2 <= N <= 5,000) equal-sized plots of land the lift will traverse and recorded the integral height H (0 <= H <= 1,000,000,000) of each plot. Safety regulations require FR to build adjacent supports no more than K (1 <= K <= N - 1) units apart. The steel between each pair of supports is rigid and forms a straight line from one support to the next. Help FR compute the smallest number of supports required such that: each segment of steel lies entirely above (or just tangent to) each piece of ground, no two consecutive supports are more than K units apart horizontally, and a support resides both on the first plot of land and on the last plot of land.
科罗拉州的罗恩打算为他的奶牛们建造一个滑雪场,虽然需要的设施仅仅是一部缆车.建造一部缆车,需要从山脚到山顶立若干根柱子,并用钢丝连结它们.你可以认为相对于地面,柱子的高度可以忽略不计.每相邻两根柱子间都有钢丝直接相连.显然,所有钢丝的任何一段都不能在地面之下. 为了节省建造的费用,罗恩希望在工程中修建尽可能少的柱子.他在准备修建缆车的山坡上迭定了N(2≤N≤5000)个两两之间水平距离相等的点,并且测量了每个点的高度H(O≤日≤10^9).并且,按照国家安全标准,相邻两根柱子间的距离不能超过K(1≤K≤N-1)个单位长度.柱子间的钢丝都是笔直的. 罗恩希望你帮他计算一下,在满足下列条件的情况下,他至少要修建多少根柱子:首先,所有的柱子都必须修建在他所选定的点上,且每一段钢丝都必须高于地面或者正好跟地面相切.相邻两根柱子的距离不大于K个单位长度.当然,在第一个点与最后一个点上一定都要修建柱子.
Input
Line 1: Two space-separate integers, N and K
Lines 2..N+1: Line i+1 contains a single integer that is the height of plot i.
第1行:两个整数N和K,用空格隔开.
第2到N+1行:每行包括一个正整数,第i+l行的数描述了第i个点的高度.
Output
- Line 1: A single integer equal to the fewest number of lift towers FR needs to build subject to the above constraints
输出一个整数,即罗恩最少需要修建的柱子的数目.
Sample Input 1
13 4
0
1
0
2
4
6
8
6
8
8
9
11
12
Sample Output 1
5
思路:
dp状态转移方程:
dp[i] 表示在i点最少建立的柱子
dp[i] 就等于 dp[i-k] ~ dp[i-1] 上建立的最少的柱子数 + 1
计算斜率,判断两个点是否能连接
AC代码
#include<bits/stdc++.h>
#define eps 1e-6
using namespace std;
typedef long long ll;
const int maxn = 5010;
int n,k;
ll h[maxn];
ll dp[maxn];
/*
线性dp状态转移方程:
dp[i] 表示在i点最少建立的柱子
dp[i] 就等于 dp[i-k] ~ dp[i-1] 上建立的最少的柱子数 + 1
*/
//判断起点e 和 终点s 是否能建立一个连线 (当且仅当 e 和 s连起来的线段上没有经过其他柱子)
bool check(int s,int e){
double h1=h[s],k=(h[e]-h[s])*1.0/(e-s); //k表示斜率 h表示当前高度
for(int i=s+1;i<e;i++){
h1+=k;
if(h1<h[i]&&fabs(h1-h[i])>eps)return 0;
}
return true;
}
int main(){
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%lld",&h[i]);
memset(dp,0x3f3f3f,sizeof(dp));//要求dp的最小值 先要初始化为无穷大
dp[1] = 1;
dp[2] = 2;
for(int i=3;i<=n;i++){
for(int j=max(1,i-k);j<i;j++){
if(dp[i] > dp[j] + 1 && check(j,i)) dp[i] = dp[j] + 1;
}
}
printf("%lld\n",dp[n]);
return 0;
}
BZOJ-1721|线性dp-缆车支柱的更多相关文章
- LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
- hdu1712 线性dp
//Accepted 400 KB 109 ms //dp线性 //dp[i][j]=max(dp[i-1][k]+a[i][j-k]) //在前i门课上花j天得到的最大分数,等于max(在前i-1门 ...
- 动态规划——线性dp
我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...
- POJ 2479-Maximum sum(线性dp)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33918 Accepted: 10504 Des ...
- poj 1050 To the Max(线性dp)
题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...
- nyoj44 子串和 线性DP
线性DP经典题. dp[i]表示以i为结尾最大连续和,状态转移方程dp[i] = max (a[i] , dp[i - 1] + a[i]) AC代码: #include<cstdio> ...
- 『最大M子段和 线性DP』
最大M子段和(51nod 1052) Description N个整数组成的序列a[1],a[2],a[3],-,a[n],将这N个数划分为互不相交的M个子段,并且这M个子段的和是最大的.如果M &g ...
- 『最长等差数列 线性DP』
最长等差数列(51nod 1055) Description N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 5 6 8 9 10 12 13 14 等差子数列包括(仅包括两项的不 ...
随机推荐
- HANA SQL备忘录
1.改变元素列类型 ALTER TABLE <TABLE_NAME> ALTER (<COLUMN_NAME> <COLUMN_TYPE>);
- new和malloc区别,delete和delete []区别
面试被问到上述问题,所以特地总结一下: 一.new和malloc的区别. 1.new可以返回指定类型的指针,并且自动分配内存大小:malloc需要计算手动计算分配空间的大小,并且返回值需要强转为实际类 ...
- Centos 安装Java jdk
1/ yum search java|grep jdk 2/ yum install java-......... 3/ vi /etc/profile 在最后添上: 4/ source /etc/p ...
- RedHat Enterprise Linux 6.4使用网易Centos 6 的yum源
1.首先到http://mirrors.163.com/centos下载软件包 x86 地址:http://mirrors.163.com/centos/6/os/i386/Packages/ x86 ...
- 加壳软件-Virbox Protector Standalone
Virbox Protector Standalone 加壳工具 防止代码反编译,更安全,更方便 产品简介 Virbox Protector Standalone提供了强大的代码虚拟化.高级混淆与智能 ...
- 线程等待(java)
定义一个对象: private Object objWaiter = new Object(); 在需要等待的地方加等待锁: synchronized (objWaiter) { objWaiter. ...
- 【Python】This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck
情况一:导包import时发生错误,请参考这两位 https://blog.csdn.net/zhangyu4863/article/details/80212068https://www.cnblo ...
- 【Pattern】-NO.150.Pattern.1 -【Pattern UML】
Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...
- CentOS 7下 部署Redis-cluster集群
redis集群是一个无中心的分布式redis存储架构,可以在多个节点之间进行数据共享,解决了redis高可用.可扩展等问题,redis集群提供了以下两个好处:1)将数据自动切分(split)到多个节点 ...
- C# 正则表达式提取字符串中括号里的值
version = Regex.Replace(str, @"(.*\()(.*)(\).*)", "$2"); //小括号() Regex rgx = new ...