您的位置:首页 > 其它

NYOJ 1070 诡异的电梯【Ⅰ】 DP

2016-02-02 12:10 288 查看


诡异的电梯【Ⅰ】

时间限制:1000 ms  |  内存限制:65535 KB
难度:3

描述
新的宿舍楼有 N(1≤N≤100000) 层 and
M(1≤M≤100000)个学生. 在新的宿舍楼里, 为了节约学生的时间也为了鼓励学生锻炼身体, 所以规定该宿舍楼里的电梯在相邻的两层之间是不会连续停下(即,如果在第2层停下就不能在第3层停下。).所以,如果有学生在相邻的两层之间要停下, 则其中的一部分学生必须选择走楼梯来代替。规定:一个人走下一层楼梯的花费为A,走上一层楼梯的花费为B。(1≤A,B≤100)现在请你设计一个算法来计算出所有学生走楼梯花费的最小费用总和。 所有的学生一开始都在第一层,电梯不能往下走,在第二层的时候电梯可以停止。

输入输入有几组数据T。T(1≤T≤10)

每组数据有N (1≤N≤100000),M(1≤M≤100000),A,B(1≤A,B≤100)。

接下来有M个数字表示每个学生想要停的楼层。

输出输出看样例。
样例输入
1
3 2 1 1
2 3


样例输出
Case 1: 1


提示原题:

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.

来源翻译【2014湘潭邀请赛】
上传者ACM_钟诗俊

思路:dp[i]表示前i层楼的最小花费。

代码:

#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 100005;
const int MAXN = 2005;
const int MAXM = 200010;
const int N = 1005;

int n,m,a,b;
int num[maxn],dp[maxn];

int main()
{
//#ifndef ONLINE_JUDGE
// freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
//#endif
int i,j,t,x,cas=0;
scanf("%d",&t);
while (t--)
{
scanf("%d%d%d%d",&n,&m,&a,&b);
int mi=min(a,b);
memset(num,0,sizeof(num));
for (i=0;i<m;i++)
{
scanf("%d",&x);
num[x]++;
}
memset(dp,INF,sizeof(dp));
dp[0]=dp[1]=dp[2]=0;
for (i=3;i<=n;i++)
{
dp[i]=min(dp[i-1]+a*num[i],dp[i-2]+num[i-1]*mi);
// int tmp=min(num[i-1]*a*2+num[i-2]*a,num[i-1]*b+num[i-2]*b*2);
// tmp=min(tmp,num[i-1]*b+num[i-2]*a);
// tmp=min(tmp,num[i-1]*a*2+num[i-2]*b*2);
// dp[i]=min(dp[i-3]+tmp,dp[i]);
// dp[i]=min(dp[i],dp[i-2]+num[i-1]*mi);
}
printf("Case %d: %d\n",++cas,dp
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息