BAPC 2014 Preliminary 暑假第一场 D: Lift Problems

Lift Problems题目链接
代码参考:http://www.cnblogs.com/tingtin/archive/2018/07/08/9279819.html
On the ground floor (floor zero) of a large university building a number of students are waiting
for a lift. Normally, the lift stops at every floor where one or more students need to get
out, but that is annoying for the students who want to get out on a higher floor. Alternatively,
the lift could skip some floors, but that is annoying for the students who wanted to get out on
one of those floors.
Specifically, a student will be annoyed on every floor where the lift stops, if the lift has not
yet reached the floor on which he or she wants to get out. If the lift skips the floor on which
a student wants to get out, he or she will be annoyed on that floor and every higher floor, up
to (and excluding) the floor where the lift makes its next stop and the student can finally get
out to start walking back down the stairs to his or her destination.
For example, if a student wants to get out on the fifth floor, while the lift stops at the
second, seventh and tenth floor, the student will be annoyed on floors two, five and six. In
total, this student will thus be annoyed on three floors.
Upon entering the lift, every student presses the button corresponding to the floor he or
she wants to go to, even if it was already pressed by someone else. The CPU controlling the
lift thus gets to know exactly how many students want to get out on every floor.
You are charged with programming the CPU to decide on which floors to stop. The goal
is to minimize the total amount of lift anger: that is, the number of floors on which every
student is annoyed, added together for all students.
You may ignore all the people who may (want to) enter the lift at any higher floor. The
lift has to operate in such a way that every student waiting at the ground floor can reach the
floor she or he wants to go to by either getting out at that floor or by walking down the stairs.
Input
On the first line one positive number: the number of test cases, at most 100. After that per test
case:
• one line with a single integer n (1 ≤ n ≤ 1 500): the number of floors of the building,
excluding the ground floor.
• one line with n space-separated integers si (0 ≤ si ≤ 1 500): for each floor i, the number
of students si that want to get out.
Output
Per test case:
• one line with a single integer: the smallest possible total amount of lift anger.
8 Problem D: Lift Problems
Sample in- and output

样例输入
3
5
0 3 0 0 7
5
0 0 3 0 7
10
3 1 4 1 5 9 2 6 5 3
样例输出
7
6
67

题意:坐电梯计算愤怒值。动态规划问题。
电梯层数数据有1~1500,给你电梯的层数和在每一层(从1-n)要离开的人数。
当电梯未到达学生想要离开的楼层就停,学生愤怒值增加1,当电梯跳过学生想要离开的楼层,学生在那个楼层愤怒值加1,并且在每个更高的楼层愤怒值都加1,直到到达电梯下一站要停的楼层。
比如:学生要在5楼离开,而电梯在2,7,10层停,则他会在第二层,第五层,第六层增加愤怒值,所以该学生愤怒值为3.
如第一个测试样例:
5
0 3 0 0 7
当在第二层,第七层停的时候愤怒值最小,为7,若只在第七层停则愤怒值为3+3+3=9;
让你管理cpu,决定在哪些楼层停,使得结果愤怒值最小,结果输出最小愤怒值。

动态规划:
这里写图片描述

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define maxn 1600
#define inf 0x3f3f3f3f
int T;
int n,s[maxn];
int dp[maxn];// 在第i层下楼时从一楼到i楼生气的最小值
using namespace std;

int main(){
    cin>>T;
    while(T--){
        cin>>n;
        int sum=0;
        for(int i=1;i<=n;i++){
            cin>>s[i];
            sum+=s[i]; //sum为高层人数
        }
        dp[0]=0;
        int k;
        for(int i=1;i<=n;i++){
            sum-=s[i];  //i以前的都会下,sum为i以上的
            k=0;
            dp[i]=inf;
            for(int j=i-1;j>=0;j--){
                dp[i]=min(dp[i],dp[j]+k+sum);
                k=k+(i-j)*s[j];
            }
        }
        cout<<dp[n]<<endl;
    }
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值