日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Telephone Wire(POJ-3612)

發布時間:2025/3/17 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Telephone Wire(POJ-3612) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Problem Description

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.

Input

Line 1: Two space-separated integers: N and C
Lines 2..N+1: Line i+1 contains a single integer: heighti

Output

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

Sample Input

5 2

2
3
5
1
4

Sample Output

15

題意:要改造n個電線桿,相鄰兩電線桿改造費用=C*兩電線桿的高度差,也可對任意電線桿進行加長x,但要花費加長費用x^2,求改造電線桿的最少的費用。

思路

用f[i][j]表示第i棵樹高度為j的時候的最小代價,枚舉相鄰兩棵樹高度即可,狀態方程:f[i][j]=min(f[i][k]+abs(j-k)+(j-a[i])^2),測試后發現超時,不知道怎么優化,看了他人題解。

對狀態方程進行優化,利用分類討論j<k和j>=k,將k維度用兩個數組預處理,即:high[j]=min(f[i-1][k]-k*c) (j>=k),low[j]=min(f[i-1][k]+k*c) (j<k),原狀態方程就變為:f[i][j]=(j-a[i])^2+min(high[j]+j*c,low[j]-j*c);

Source Program

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<string> #include<cstdlib> #include<queue> #include<vector> #define INF 0x3f3f3f3f #define PI acos(-1.0) #define N 105 #define MOD 100001 #define E 1e-12 using namespace std; int low[N],high[N],f[N]; int main() {int n,m;while(scanf("%d%d",&n,&m)!=EOF){int a;scanf("%d",&a);for(int i=1;i<=100;i++){if(i<a)f[i]=INF;elsef[i]=(a-i)*(a-i);}for(int i=1;i<n;i++){int temp=INF;scanf("%d",&a);for(int j=100;j>0;j--)//對low預處理{temp=min(temp,f[j]+j*m);low[j]=temp;}for(int j=1;j<=100;j++)//對high預處理{temp=min(temp,f[j]-j*m);high[j]=temp;f[j]=INF;}for(int j=a;j<=100;j++)f[j]=(j-a)*(j-a)+min(low[j]-j*m,high[j]+j*m);}int ans=INF;for(int i=1;i<=100;i++)ans=min(ans,f[i]);printf("%d\n",ans);} }

?

總結

以上是生活随笔為你收集整理的Telephone Wire(POJ-3612)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。