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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

XTU Dormitory's Elevator(动态规划)

發布時間:2025/3/16 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 XTU Dormitory's Elevator(动态规划) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Dormitory's Elevator

Accepted : 70?Submit : 394
Time Limit : 1000 MS?Memory Limit : 65536 KB

Problem Description

The new dormitory has N(1≤N≤100000) floors and M(1≤M≤100000)students. In the new dormitory, in order to save student's time as well as encourage student exercise, the elevator in dormitory will not stop in adjacent floor. So if there are people want to get off the elevator in adjacent floor, one of them must walk one stair instead. Suppose a people go down 1 floor costs A energy, go up 1 floor costs B energy(1≤A,B≤100). Please arrange where the elevator stop to minimize the total cost of student's walking cost.All students and elevator are at floor 1 initially, and the elevator can not godown and can stop at floor 2.

Input

First line contain an integer T, there are T(1≤T≤10) cases. For each case T, there are two lines. First line: The number of floors N(1≤N≤100000), and the number of students M(1≤M≤100000),A,B(1≤A,B≤100) Second line: M integers (2≤A[i]≤N), the student's desire floor.

Output

Output case number first, then the answer, the minimum of the total cost of student's walking cost.

Sample Input

1 3 2 1 1 2 3

Sample Output

Case 1: 1

解題思路:這個題最開始想開二維dp[i][j],一看數據就pass了。然后想了下,dp[i]表示電梯停到第i層樓時,學生到1-i層樓的最小化費。這樣為了保證最小花費,肯定要盡可能多的使用電梯,那么dp[i]的狀態首先肯定是可以從dp[i-2]轉移過來的,如果僅僅只有dp[i-2],那么電梯就可能會停到2-4-6.....,中間停奇數層的可能就被忽略了。所以會有dp[i-3]的情況,保證了奇偶層都可能停,dp[i-4]就沒有必要了,因為肯定會坐電梯上來的。這樣在第i層,i-1層,i-2層之間進行狀態轉移。詳見代碼。 #include<iostream> #include<cstdio> #include<cstring> using namespace std;const int maxn = 100005; int n,m,A,B,dp[maxn],F[maxn],cnt[maxn];int MIN(int a,int b,int c,int d) {if(a <= b && a <= c && a <= d) return a;if(b <= a && b <= c && b <= d) return b;if(c <= a && c <= b && c <= d) return c;if(d <= a && d <= b && d <= c) return d; }int main() {int t,cas = 1;scanf("%d",&t);while(t--){scanf("%d%d%d%d",&n,&m,&A,&B);memset(cnt,0,sizeof(cnt));memset(dp,0,sizeof(dp));for(int i = 1; i <= m; i++){scanf("%d",&F[i]);cnt[F[i]]++;}dp[1] = dp[2] = 0;dp[3] = min(A,B)*cnt[2];for(int i = 4; i <= n; i++){dp[i] = dp[i-2] + min(A,B)*cnt[i-1];int tmp1 = cnt[i-1]*A + cnt[i-2]*2*A; //從i樓下到i-1和i-2樓int tmp2 = cnt[i-1]*2*B + cnt[i-2]*B; //從i-3樓上到i-1和i-2樓int tmp3 = cnt[i-1]*A + cnt[i-2]*B; //i樓下到i-1樓,i-3樓上到i-2樓int tmp4 = cnt[i-2]*2*A + cnt[i-1]*2*B; //i樓下到i-2樓,i-3樓上到i-1樓dp[i] = min(dp[i],dp[i-3] + MIN(tmp1,tmp2,tmp3,tmp4));}int ans = dp[n];ans = min(ans,dp[n-1]+cnt[n]*B);ans = min(ans,dp[n-2]+cnt[n]*2*B + cnt[n-1]*B);printf("Case %d: %d\n",cas++,ans);}return 0; }

總結

以上是生活随笔為你收集整理的XTU Dormitory's Elevator(动态规划)的全部內容,希望文章能夠幫你解決所遇到的問題。

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