poj 3612

//先贴代码:
#include<iostream>
#include<cstdio>
#include<string.h>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;

const int maxn = 100005;
const int maxh = 101;
const int inf = 0x7fffffff;
int h[maxn];
int n, c;
int dp[2][maxh];
int DP(){
	int t = 0;
	for(int i = h[1]; i < maxh; ++i){
		dp[t][i] = pow(i - h[1],2);
	}
	for(int i = 2; i <= n; ++i){ //enumerate i
		int mk[maxh],ak[maxh];
		for(int k = 0; k < maxh; ++k){
			dp[1-t][k] = inf;
			mk[k] = dp[t][k] == inf ? inf:(dp[t][k] - c * k);
			ak[k] = dp[t][k] == inf ? inf:(dp[t][k] + c * k);
		}
		int mmin = inf, amin = inf;
		int minrec[maxh], addrec[maxh];
		for(int j = 0; j < maxh; ++j){
			minrec[j] = j == 0 ? 0 : (mk[j] < mk[minrec[j - 1]] ? j: minrec[j-1]);
			addrec[maxh - j - 1] = (j == 0) ? (maxh - 1) : ((ak[maxh - j - 1] < ak[addrec[maxh - j]]) ? (maxh - j - 1): addrec[maxh - j]);
		}
		for(int j = h[i-1]; j < maxh; ++j){
			mmin = mk[minrec[j]];
			amin = ak[addrec[j]];
			int sub = pow(j - h[i],2);
			dp[1-t][j] = min(mmin + c * j + sub,amin - c * j + sub);
		}
		amin = ak[addrec[h[i-1]]];
		for(int j = h[i]; j < h[i-1]; ++j){
			dp[1-t][j] = min(dp[1-t][j],amin - c * j +((int)pow(j - h[i],2)));
		}
		for(int j = 0; j < h[i]; ++j){
			dp[1-t][j] = inf;
		}
		t = 1 - t;
	}
	return 0;
}

int main(){
	while(scanf("%d%d",&n, &c)!=EOF){
		for(int i = 1; i <= n; ++i){
			scanf("%d", h + i);
		}
		for(int i = 0; i < 2; ++i){
			for(int j = 0; j < maxh; ++j){
				dp[i][j] = inf;
			}
		}
		DP();

		int mincost = 0x7fffffff;
		int choic = n & 1;
		choic = 1 - choic;
		for(int i = h[n]; i < maxh; ++i){
			mincost = min(mincost, dp[choic][i]);
		}
		printf("%d\n", mincost);
	}
	return 0;
}

题意:给一个杆子的高度的数组h[1] ... h[n], 在相邻两个杆子(首尾的杆子不看作相邻)之间布线,单位高度差的费用为c;布线前可以选择将杆子的高度提高,提高x单位高度时,费用为x * x;

求在这n个杆子之间布线后的最小费用

思路:动态规划

dp[i][j] 表示第i根杆子的高度为j是,前i根杆子的最小费用和,那么状态转移方程为

dp[i][j] = min{dp[i-1][k] + c * |j -k| + pow(j - h[i],2)}

j,k的枚举范围是100,i有100000个, 复杂度太高,仔细观察可以发现

dp[i][j] = min{ dp[i-1][k] - c * k + c * j + pow(j - h[i],2) a[i-1]<= k <= j

dp[i-1][k] + c * k - c * j + pow(j - h[i],2) j <= k <= 100 }

这样,就可以分别用minrec和addrec两个数组纪录从0到j里使得dp[i-1][k]-c*k最小和从j到100里使得d[i-1][k] + c *k最小的k(这个纪录的时间是O(maxh)的,而不是对每个j 都枚举k)

优化:1. 对每个杆子,枚举高度时的上限可以是输入数组中的最大值而不是100

2. minrec和addrec直接纪录和j相关的最优值而不是产生最优值的下标

时间: 2024-08-26 05:36:51

poj 3612的相关文章

bzoj 1705;poj 3612:[Usaco2007 Nov]Telephone Wire 架设电话线

Description 最近,Farmer John的奶牛们越来越不满于牛棚里一塌糊涂的电话服务 于是,她们要求FJ把那些老旧的电话线换成性能更好的新电话线. 新的电话线架设在已有的N(2 <= N <= 100,000)根电话线杆上, 第i根电话线杆的高度为height_i米(1 <= height_i <= 100). 电话线总是从一根电话线杆的顶端被引到相邻的那根的顶端 如果这两根电话线杆的高度不同,那么FJ就必须为此支付 C*电话线杆高度差(1 <= C <=

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is

POJ——T2446 Chessboard

http://poj.org/problem?id=2446 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18560   Accepted: 5857 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of c

poj 1088 滑雪 DP(dfs的记忆化搜索)

题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 求最大的下滑路径 分析:因为只能从高峰滑到低峰,无后效性,所以每个点都可以找到自己的最长下滑距离(只与自己高度有关).记忆每个点的最长下滑距离,当有另一个点的下滑路径遇到这个点的时候,直接加上这个点的最长下滑距离. dp递推式是,dp[x][y] = max(dp[x][y],dp[x+1][y]+

POJ 1385 计算几何 多边形重心

链接: http://poj.org/problem?id=1385 题意: 给你一个多边形,求它的重心 题解: 模板题,但是不知道为啥我的结果输出的确是-0.00 -0.00 所以我又写了个 if (ans.x == 0) ans.x = 0 感觉好傻逼 代码: 1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <stack> 6

POJ 1741 Tree(树的点分治,入门题)

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v)=The min distance between node u and v.Give an in

poj 1655 树的重心

Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13178   Accepted: 5565 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or m