BAPC - Lift Problems

传送门:https://nanti.jisuanke.com/t/A1732

On the ground floor (floor zero) of a large university building a number of students are wait- ing 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.

输入格式

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 (5001≤n≤1500): the number of floors of the building, excluding the ground floor.
  • one line with n space-separated integers si​ (5000≤si​≤1500): for each floor i, the number of students si​ that want to get out.

输出格式

Per test case:

  • one line with a single integer: the smallest possible total amount of lift anger.

样例输入

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

题意:题目意思很好理解,电梯从0层开始,把人全送达该去的层数(比如A要去X楼)。期间如果电梯停在Y(!=X)楼,如果Y<X怒气值+1,否则怒气值+(Y-X)。计算所有人的最少怒气值。

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int t,n,a[1551],f[1551]={0},s;
//每i层发火只有两种可能,没到的人(s),超过的人(i-j)*a[j];
int main()
{
    cin>>t;
    while(t--)
    {
        s=0;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            s+=a[i];
        }
        for(int i=1;i<=n;i++)
        {
            s-=a[i];
            int k=0;
            f[i]=inf;
            for(int j=i-1;j>=0;j--)
            {
                f[i]=min(f[i],f[j]+k+s);
                k+=(i-j)*a[j];
            }
        }
        cout<<f[n]<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值