Xiangtan University OJ 1206 dormitory's elevator DP

Source: Internet
Author: User
27153 Njczy2010 1206 Accepted 1976 KB 234 MS G ++ 1415 B 10:01:23

 

Really vomit blood AC, so easy question .....

 

Dormitory's elevator
Accepted: 46   Submit: 302
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 ≤, 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
13 2 1 12 3
Sample output
Case 1: 1
Sourcedaizhenyang

 

 

A Indicates going upstairs ?????? I still haven't figured out what the situation is ....

Okay, well, I understand .... Burst into tears ....

 

 

Problem resolved from: http://blog.csdn.net/y990041769/article/details/39343269

Analysis: This is actually a simple one-dimensional DP, using DP [I] to represent the minimum physical strength required from the first layer to the second layer.

Because you cannot stay in the adjacent floor, so you can transfer from DP [I-2], but this is not the best but also from DP [I-3], because so you can reach all the floors. We only need to optimize the DP among all.

One of the other conditions to note is that when transferred from DP [I-3], there are four options for people in the middle of the two layers:

1: go up or go down

2: The upper and lower layers

3: There are two lower layers and two lower layers. (this was not taken into account at the time. Be careful)

The code is easy to write,

 

 

 1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<string>10 11 #define N 10000512 #define M 1513 #define mod 1000000714 //#define p 1000000715 #define mod2 10000000016 #define ll long long17 #define LL long long18 #define maxi(a,b) (a)>(b)? (a) : (b)19 #define mini(a,b) (a)<(b)? (a) : (b)20 21 using namespace std;22 23 int T;24 int n,m;25 int cnt[N];26 int a,b;27 int dp[N];28 int x;29 int ans;30 int c;31 32 void ini()33 {34     ans=0;35     memset(cnt,0,sizeof(cnt));36     memset(dp,0x3f3f3f3f,sizeof(dp));37     scanf("%d%d%d%d",&n,&m,&b,&a);38     c=min(a,b);39     for(int i=1;i<=m;i++){40         scanf("%d",&x);41         cnt[x]++;42     }43     dp[0]=0;44     dp[1]=0;45     dp[2]=0;46     dp[3]=c*cnt[2];47 48 }49 50 51 void solve()52 {53     int i;54     for(i=4;i<=n;i++){55         dp[i]=min(dp[i-2]+c*cnt[i-1],dp[i-3]+cnt[i-2]*min(a,2*b)+cnt[i-1]*min(b,2*a));56     }57     ans=min(dp[n],dp[n-1]+cnt[n]*a);58 }59 60 void out()61 {62     printf("%d\n",ans);63 }64 65 int main()66 {67     //freopen("data.in","r",stdin);68     //freopen("data.out","w",stdout);69     scanf("%d",&T);70     for(int ccnt=1;ccnt<=T;ccnt++)71    // while(T--)72    // while(scanf("%d%d",&n,&m)!=EOF)73     {74       //  if(n==0 && m==0) break;75         printf("Case %d: ",ccnt);76         ini();77         solve();78         out();79     }80 81     return 0;82 }

 

Xiangtan University OJ 1206 dormitory's elevator DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.