「USACO13MAR」「LuoguP3080」 牛跑The Cow Run (區間dp
題目描述
Farmer John has forgotten to repair a hole in the fence on his farm, and his N cows (1 <= N <= 1,000) have escaped and gone on a rampage! Each minute a cow is outside the fence, she causes one dollar worth of damage. FJ must visit each cow to install a halter that will calm the cow and stop the damage.
Fortunately, the cows are positioned at distinct locations along a straight line on a road outside the farm. FJ knows the location P_i of each cow i (-500,000 <= P_i <= 500,000, P_i != 0) relative to the gate (position 0) where FJ starts.
FJ moves at one unit of distance per minute and can install a halter instantly. Please determine the order that FJ should visit the cows so he can minimize the total cost of the damage; you should compute the minimum total damage cost in this case.
農夫約翰的牧場圍欄上出現了一個洞,有N(1 <= N <= 1,000)只牛從這個洞逃出了牧場。這些出逃的奶牛很狂躁,他們在外面到處搞破壞,每分鐘每頭牛都會給約翰帶來1美元的損失。約翰必須用韁繩套住所有的牛,以停止他們搞破壞。
幸運的是,奶牛們都在牧場外一條筆直的公路上,牧場的大門恰好位於公裏的0點處。約翰知道每頭牛距離牧場大門的距離P_i(-500,000 <= P_i <= 500,000, P_i != 0)
約翰從農場大門出發,每分鐘移動一個單位距離,每到一頭牛所在的地點,約翰就會給它套上韁繩,套韁繩不花時間。按怎樣的順序去給牛套韁繩才能使約翰損失的費用最少?
輸入輸出格式
輸入格式:* Line 1: The number of cows, N.
* Lines 2..N+1: Line i+1 contains the integer P_i.
輸出格式:* Line 1: The minimum total cost of the damage.
輸入輸出樣例
輸入樣例#1: 復制4 -2 -12 3 7輸出樣例#1: 復制
50
說明
Four cows placed in positions: -2, -12, 3, and 7.
The optimal visit order is -2, 3, 7, -12. FJ arrives at position -2 in 2 minutes for a total of 2 dollars in damage for that cow.
He then travels to position 3 (distance: 5) where the cumulative damage is 2 + 5 = 7 dollars for that cow.
He spends 4 more minutes to get to 7 at a cost of 7 + 4 = 11 dollars for that cow.
Finally, he spends 19 minutes to go to -12 with a cost of 11 + 19 = 30 dollars.
The total damage is 2 + 7 + 11 + 30 = 50 dollars.
題解
不知道為什麽能藍。
這是一類被我們機房稱為老王關燈的題鬼嘞只有我這麽叫
老(wan)王(e)關(zhi)燈(yuan)
這道題比老王關燈還簡單一丟丟,不用算功率啦,
單位時間消耗的dollar直接算未覆蓋區間就好啦~
然後這種題 開個long long開不出吃虧開不出上當
還能有效預防越界(QAQ!!!!)
1 /* 2 qwerta 3 P3080 [USACO13MAR]牛跑The Cow Run 4 Accepted 5 100 6 代碼 C++,0.81KB 7 提交時間 2018-09-22 18:59:00 8 耗時/內存 9 231ms, 16760KB 10 */ 11 #include<cmath> 12 #include<cstdio> 13 #include<cstring> 14 #include<iostream> 15 #include<algorithm> 16 using namespace std; 17 int dis[1007]; 18 long long f[1007][1007][2]; 19 int main() 20 { 21 //freopen("a.in","r",stdin); 22 int n; 23 scanf("%d",&n); 24 for(int i=1;i<=n;++i) 25 scanf("%d",&dis[i]); 26 n++; 27 sort(dis+1,dis+n+1); 28 memset(f,127,sizeof(f)); 29 int x=0; 30 for(int i=1;i<=n;++i) 31 if(dis[i]==0)x=i; 32 f[x][x][0]=f[x][x][1]=0; 33 for(int len=2;len<=n;++len) 34 for(int l=1,r=len;r<=n;++l,++r) 35 { 36 f[l][r][0]=min(f[l+1][r][0]+(dis[l+1]-dis[l])*(n-len+1), 37 f[l+1][r][1]+(dis[r]-dis[l])*(n-len+1)); 38 f[l][r][1]=min(f[l][r-1][0]+(dis[r]-dis[l])*(n-len+1), 39 f[l][r-1][1]+(dis[r]-dis[r-1])*(n-len+1)); 40 } 41 cout<<min(f[1][n][0],f[1][n][1]); 42 return 0; 43 }
「USACO13MAR」「LuoguP3080」 牛跑The Cow Run (區間dp