Nyist 1070 weird elevator [I]

Source: Internet
Author: User

Weird elevator [I]
Time Limit: 1000 MS | memory limit: 65535 KB
Difficulty: 3

Description
The new dormitory has n (1 ≤ n ≤ 100000) floors and M (1 ≤ m ≤ 100000) students. in the new dormitory, in order to save time for students and encourage students to exercise, the elevators in the dormitory building do not stop continuously between the adjacent two floors (that is, if you stop at Layer 3, you cannot stop at Layer 2nd .). Therefore, if a student stops between two adjacent layers, some of the students must choose to take the stairs instead. Rule: the cost of a person walking down a stair is A, and the cost of walking down a stair is B. (1 ≤ a, B ≤ 100) Now you need to design an algorithm to calculate the sum of the minimum costs for all students to take the stairs. All the students are on the first floor, the elevator cannot go down, and the elevator can stop at the second floor.

 

Input
The input contains several groups of data t. T (1 ≤ T ≤ 10)
Each group has n (1 ≤ n ≤ 100000), m (1 ≤ m ≤ 100000), a, B (1 ≤ a, B ≤ 100 ).
Next there are m numbers indicating the floor that each student wants to stop.

Output
Sample output.


Sample Input
1
3 2 1 1
2 3
Sample output
Case 1: 1


Prompt
Original question:
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.


Output:
Output case number first, then the answer, the minimum of the total cost of student's walking cost.
Source
Translation [2014 Xiangtan Invitational competition]


Uploaded
ACM _ Zhong Shijun

 

Problem solving: dynamic planning. DP [I] indicates stopping at the I layer. Now, assuming stopping at the I layer, the last stop could have been a I-2 or a I-3.

Suppose it's a I-2. So the people who go to the I-1 can do I next layer, or directly in the I-2 on a layer, min (Up, down) * A [I-1]

 

Suppose in the I-3 layer stop, then the situation is that I-2 people can do I down two layers, or from the I-3 onto a layer, the same I-1 people...

 

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 100100;18 int a[maxn],dp[maxn],up,down,n,m;19 int main() {20     int t,tmp,cs = 1;21     scanf("%d",&t);22     while(t--){23         scanf("%d %d %d %d",&n,&m,&down,&up);24         memset(dp,0x3f,sizeof(dp));25         memset(a,0,sizeof(a));26         for(int i = 0; i < m; ++i){27             scanf("%d",&tmp);28             a[tmp]++;29         }30         dp[0] = dp[1] = dp[2] = 0;31         for(int i = 3; i <= n; ++i){32             dp[i] = min(dp[i],dp[i-2]+min(up,down)*a[i-1]);33             int x = min(a[i-1]*down,a[i-1]*up*2);34             int y = min(a[i-2]*up,a[i-2]*down*2);35             dp[i] = min(dp[i],dp[i-3]+x+y);36         }37         printf("Case %d: %d\n",cs++,dp[n]);38     }39     return 0;40 }
View code

 

Nyist 1070 weird elevator [I]

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.