D:Lift Problems

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 (1≤n≤1500): the number of floors of the building, excluding the ground floor.
  • one line with n space-separated integers si​ (0≤si≤15000): 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
枚举法,如果当前电梯停的话,则此时的愤怒值为上次电梯停时到现在中间所有人的持续愤怒和当前到顶层所有人的一次愤怒(动态规划)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int t,n,a[],ans[];
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]+=a[i-];
}
ans[]=;
for(int i=;i<=n;i++)
{
ans[i]=INF;
for(int j=i-;j>=;j--)
{
ans[i]=min(ans[i],ans[j]+a[n]-a[i]);
ans[j]+=a[i]-a[j];
}
// printf("%d ",ans[i]);
} printf("%d\n",ans[n]);
}
return ;
}

最新文章

  1. Windows快捷键
  2. PHP慢脚本日志和Mysql的慢查询日志
  3. PHP字符串处理
  4. 最难面试的IT公司之ThoughtWorks代码挑战——FizzBuzzWhizz游戏(C#解法)
  5. Web后台技术趋势
  6. XML类似的解析时,会遇到&#39;XXX&#39; 不是 &#39;NCName&#39; 的有效值的问题
  7. 使用jQuery设置disabled属性与移除disabled属性
  8. CentOS6 Squid代理服务器的安装与配置
  9. jQuery中filter(),not(),split()的用法
  10. java调用shell脚本
  11. java学习笔记之String类
  12. 竞品调研时发现的Android新设计特性
  13. JTA事务管理
  14. mapreduce运行原理及YARN
  15. c++ 接口类
  16. atexit()使用
  17. MFC中onmouseover与onmousemove的区别
  18. PowerDesigner删除外键关系,而不删除外键列[转]
  19. zookeeper 高可用集群搭建
  20. 「Ionic」WebStorm的使用錯誤-

热门文章

  1. 【BUG】android.content.res.Resources$NotFoundException: File res/drawable-xxhdpi/toolbar_line.png from
  2. php设计模式之工厂方法模式
  3. CNN tflearn处理mnist图像识别代码解说——conv_2d参数解释,整个网络的训练,主要就是为了学那个卷积核啊。
  4. Oracle 数据泵使用详解--精华版
  5. mount ntfs 失败解决办法
  6. Ubuntu14.04下Mongodb官网安装部署步骤(图文详解)(博主推荐)
  7. C++之虚函数表
  8. bower 代理
  9. 51nod 1785 数据流中的算法 (方差计算公式)
  10. IIS支持10万个同时请求的设置