P2885 [USACO07NOV]电话线Telephone Wire
题目描述
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
一开始自己写了个DP,居然能过样例
特别兴奋,
但是交上去只有70分
发现时间复杂度有点高
思路比较简单:
我们可以很容易的看出这道题具有无后效性,
用dp[i][j]表示前i棵树,第i棵树高度为j的最小代价
先预处理一下dp[1][j],然后对于每一棵树,我们枚举它的前一棵树的高度和这棵树的高度,
计算一下就好
时间复杂度n*h*h
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cmath>
5 #include<cstring>
6 #include<algorithm>
7 #include<queue>
8 #include<cstdlib>
9 using namespace std;
10 const int MAXN=100001;
11 const int INF =0x7f7f7f7f;
12 inline void read(int &n)
13 {
14 char c='+';bool flag=0;n=0;
15 while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
16 while(c>='0'&&c<='9')n=n*10+c-48,c=getchar();
17 }
18 int dp[MAXN][201];// 第i棵树,高度为j的最小花费
19 int n,C;
20 int a[MAXN];
21 int maxheight;
22 int main()
23 {
24 read(n);read(C);
25 memset(dp,INF,sizeof(dp));
26 for(int i=1;i<=n;i++)
27 read(a[i]),maxheight=max(maxheight,a[i]);
28 for(int i=a[1];i<=maxheight;i++)
29 dp[1][i]=(i-a[1])*(i-a[1]);
30
31 for(int i=2;i<=n;i++)//枚举所有树
32 for(int j=a[i];j<=maxheight;j++)//枚举这棵树的高度
33 for(int k=a[i-1];k<=maxheight;k++)//枚举前一棵树的高度
34 dp[i][j]=min(dp[i][j],
35 ((j-a[i])*(j-a[i])+(dp[i-1][k])+abs(j-k)*C));
36
37
38 int ans=0x7fffff;
39 for(int i=a[n];i<=maxheight;i++)
40 ans=min(ans,dp[n][i]);
41 printf("%d",ans);
42 return 0;
43 }
然后化简一下式子
1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 using namespace std;
6 const int MAXN=300005;
7 const int INF =0x7fffff;
8 const int maxheight=100;
9 int dp[301];// 第i棵树,高度为j的最小花费
10 int f[301];
11 int n,C;
12 int a[MAXN];
13 int bgsum[MAXN];
14 int edsum[MAXN];
15 int main() {
16 scanf("%d%d",&n,&C);
17 for(int i=0; i<n; i++)
18 scanf("%d",&a[i]);
19 memset(dp, 0x3f, sizeof(dp));
20 for(int i=a[0]; i<=maxheight; i++)
21 dp[i]=(i-a[0])*(i-a[0]);
22
23 for(int i=1; i<n; i++) { //枚举所有树
24 memcpy(f,dp,sizeof(dp));
25 for(int j=0; j<=maxheight; j++) dp[j]=bgsum[j]=edsum[j]=INF;
26
27 bgsum[0]=f[0];
28 for(int j=1; j<=maxheight; j++)
29 bgsum[j]=min(bgsum[j-1],f[j]-C*j);
30
31 edsum[maxheight]=f[maxheight]+maxheight*C;
32 for(int j=maxheight-1; j>=0; j--)
33 edsum[j]=min(edsum[j+1],f[j]+C*j);
34
35 for(int j=a[i]; j<=maxheight; j++) //枚举这棵树的高度
36 dp[j]=min(edsum[j]-C*j,bgsum[j]+C*j)+(j-a[i])*(j-a[i]);
37 }
38 int ans=0x7fffff;
39 for(int i=a[n-1]; i<=maxheight; i++)
40 ans=min(ans,dp[i]);
41 printf("%d",ans);
42 return 0;
43 }
- Redis3 对集群进行重新分片
- Spring Cloud Stream初窥
- jquery事件绑定性能测试
- Spring Cloud Bus整合Kafka
- 图解Mac下如何安装管理MySQL
- Spring Cloud Bus整合RabbitMQ
- 结合Scikit-learn介绍几种常用的特征选择方法(下)
- Spring Cloud Bus之RabbitMQ初窥
- 结合Scikit-learn介绍几种常用的特征选择方法(上)
- 判断图片是否加载完成
- Spring Cloud Config客户端配置细节
- Spring Cloud Config服务端配置细节(二)之加密解密
- Redis3 cluster 集群配置测试
- Spring Cloud Config服务端配置细节(一)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 论文研读-基于决策变量分析的大规模多目标进化算法
- 用python画 pareto front
- 一起来学演化计算-matlab基本数据结构struct
- 一起来学演化计算-matlab基本函数inf, isempty, round, floor, fix
- 一起来学演化计算-matlab基本函数randperm end数组索引
- 论文研读-基于决策变量聚类的大规模多目标优化进化算法
- 一起来学演化计算-matlab基本函数min
- 一起来学演化计算-matlab基本函数find
- 欧拉图和哈密顿图
- python 操作 txt 文件中数据教程[4]-python 去掉 txt 文件行尾换行
- java字符数组char[]和字符串String之间的转换
- python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件
- python操作txt文件中数据教程[1]-使用python读写txt文件
- python循环删除列表元素常见错误与正确方法
- Python字符串,整型,浮点数相互转化