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;
}

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

$$\begin{align*}f[i][j]&=(j-w_i)^2+min_{k\geq w_{i-1}}{|k-j|+f[i-1][k]}\\&=\begin{cases}(j-w_i)^2+\min\limits _{k\geq w_{i-1},k\leq j}\{f[i-1][k]+k\}\\(j-w_i)^2+\min\limits_{k\geq w_{i-1},k\geq 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值