P2885

2885

P2885 [USACO07NOV]电话线Telephone Wire

96 通过
254 提交
题目提供者 FarmerJohn2
标签 USACO2007
难度 提高+/省选-
时空限制 1s / 128MB

题目描述

Farmer John's cows are getting restless about their poor telephone service; they want FJ to replace the old telephone wire with new, more efficient wire. The new wiring will utilize N (2 ≤ N ≤ 100,000) already-installed telephone poles, each with some heighti meters (1 ≤ heighti ≤ 100). The new wire will connect the tops of each pair of adjacent poles and will incur a penalty cost C × the two poles' height difference for each section of wire where the poles are of different heights (1 ≤ C ≤ 100). The poles, of course, are in a certain sequence and can not be moved.

Farmer John figures that if he makes some poles taller he can reduce his penalties, though with some other additional cost. He can add an integer X number of meters to a pole at a cost of X2.

Help Farmer John determine the cheapest combination of growing pole heights and connecting wire so that the cows can get their new and improved service.

给出若干棵树的高度,你可以进行一种操作:把某棵树增高h,花费为h*h。

操作完成后连线,两棵树间花费为高度差*定值c。

求两种花费加和最小值。

输入输出格式

输入格式:

Line 1: Two space-separated integers: N and C

Lines 2..N+1: Line i+1 contains a single integer: heighti

输出格式:

Line 1: The minimum total amount of money that it will cost Farmer John to attach the new telephone wire.

输入输出样例

输入样例#1: 复制

5 2
2
3
5
1
4

输出样例#1: 复制

15

做法:

  • $O(nh^2)$ 的做法应该是非常好像.然后能得到 80 分.
  • 在 80 分做法的基础上加一点利用单调性的小优化就能通过
  • 在原有状态转移方程的基础上化简式子维护前缀最小值后缀最小值,可以很快通过.

做法2

#include<iostream>
#include<cstring>
#include<cstdio>
#define N 100005
#define inf 0x3f3f3f3f
using namespace std;

void read(int &s){
	char ch=getchar();
	for(;!isdigit(ch);ch=getchar());
	for(s=0;isdigit(ch);s=s*10+ch-'0',ch=getchar());
}

int n,c;
int hei[N];
int f[N][105];

int P(int a){
	return a*a;
}

int abs(int a){
	return a>0?a:-a;
}

int main(){
	read(n);read(c);
	for(int i=1;i<=n;++i)read(hei[i]);
	memset(f,inf,sizeof(f));
	for(int i=hei[1];i<=100;++i)f[1][i]=P(hei[1]-i);
	for(int i=2;i<=n;++i)
		for(int j=hei[i];j<=100;++j){
			for(int k=hei[i-1];k<=100;++k){
				int x=P(hei[i]-j)+f[i-1][k]+abs(k-j)*c;
				f[i][j]=min(f[i][j],x);
				if(x>f[i][j])break;
			}
		}
	int ans=inf;
	for(int i=0;i<=100;++i)
		ans=min(ans,f[n][i]);
	printf("%d",ans);
	return 0;
}

需要用到后缀最小值和前缀最小值
可以化简式子

$$egin{align*}f[i][j]&=(j-w_i)^2+min_{kgeq w_{i-1}}{|k-j|+f[i-1][k]}\&=egin{cases}(j-w_i)^2+minlimits _{kgeq w_{i-1},kleq j}{f[i-1][k]+k}\(j-w_i)^2+minlimits_{kgeq w_{i-1},kgeq j}{f[i-1][k]-k}end{cases}end{align*}$$

做法3

\这个做法来自于2017年SD夏令营lrh老师,下面的程序来自lgj(attack)
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int MAXN=300005;
const int INF =0x7fffff;
const int maxheight=100;
int dp[301];// 第i棵树,高度为j的最小花费
int f[301];
int n,C;
int a[MAXN];
int bgsum[MAXN];
int edsum[MAXN];
int main() {
    scanf("%d%d",&n,&C);
    for(int i=0; i<n; i++)
        scanf("%d",&a[i]);
    memset(dp, 0x3f, sizeof(dp));
    for(int i=a[0]; i<=maxheight; i++)
        dp[i]=(i-a[0])*(i-a[0]);
    for(int i=1; i<n; i++) { //枚举所有树
        memcpy(f,dp,sizeof(dp));
        for(int j=0; j<=maxheight; j++)    dp[j]=bgsum[j]=edsum[j]=INF;
        bgsum[0]=f[0];
        for(int j=1; j<=maxheight; j++)
            bgsum[j]=min(bgsum[j-1],f[j]-C*j);
        edsum[maxheight]=f[maxheight]+maxheight*C;
        for(int j=maxheight-1; j>=0; j--)
            edsum[j]=min(edsum[j+1],f[j]+C*j);
        for(int j=a[i]; j<=maxheight; j++) //枚举这棵树的高度
            dp[j]=min(edsum[j]-C*j,bgsum[j]+C*j)+(j-a[i])*(j-a[i]);
    }
    int ans=0x7fffff;
    for(int i=a[n-1]; i<=maxheight; i++)
        ans=min(ans,dp[i]);
    printf("%d",ans);
    return 0;
}

原文地址:https://www.cnblogs.com/qdscwyy/p/8321642.html