电梯问题——致敬ACM

1 second
256 megabytes
standard input
standard output
The Fair Nut lives in nn story house. aiai people live on the ii-th floor of the house. Every person uses elevator twice a day: to get from the floor where he/she lives to the ground (first) floor and to get from the first floor to the floor where he/she lives, when he/she comes back home in the evening.
It was decided that elevator, when it is not used, will stay on the xx-th floor, but xx hasn't been chosen yet. When a person needs to get from floor aa to floor bb, elevator follows the simple algorithm:
- Moves from the xx-th floor (initially it stays on the xx-th floor) to the aa-th and takes the passenger.
- Moves from the aa-th floor to the bb-th floor and lets out the passenger (if aa equals bb, elevator just opens and closes the doors, but stillcomes to the floor from the xx-th floor).
- Moves from the bb-th floor back to the xx-th.
The elevator never transposes more than one person and always goes back to the floor xx before transposing a next passenger. The elevator spends one unit of electricity to move between neighboring floors. So moving from the aa-th floor to the bb-th floor requires |a−b||a−b|units of electricity.
Your task is to help Nut to find the minimum number of electricity units, that it would be enough for one day, by choosing an optimal the xx-th floor. Don't forget than elevator initially stays on the xx-th floor.
The first line contains one integer nn (1≤n≤1001≤n≤100) — the number of floors.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1000≤ai≤100) — the number of people on each floor.
In a single line, print the answer to the problem — the minimum number of electricity units.
3
0 2 1
16
2
1 1
4
In the first example, the answer can be achieved by choosing the second floor as the xx-th floor. Each person from the second floor (there are two of them) would spend 44 units of electricity per day (22 to get down and 22 to get up), and one person from the third would spend 88units of electricity per day (44 to get down and 44 to get up). 4⋅2+8⋅1=164⋅2+8⋅1=16.
In the second example, the answer can be achieved by choosing the first floor as the xx-th floor.
题目来源:http://codeforces.com/problemset/problem/1084/A
其实,这个题目只要理解清楚意思很好写。
题目大意是说:在一个n层的大楼里的住户每天要上下楼各一次,电梯每移动一层消耗一点电能,求:最少需要消耗的电能。
首先,要清楚题目中的第x层是一个未知的楼层。也就是说,需要找到最优的第x层得出最后的答案。有了这样的思路,题目就很好写了。
其次,注意题目本意是第x层需要返回。也就是说,每次执行上下楼层后都要返回第x层。
最后,话就不多说了,暴力可以解决的问题从来不是问题,如果是问题,那就是不够暴力。代码如下:
#include<stdio.h>
#include<algorithm>
using namespace std; int main()
{
int a[];//存各楼层人数
int n;//楼层数
long long sum,ans=0x3f3f3f3f;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=;i<=n;i++)//改变x的值
{
sum=;
for(int j=;j<=n;j++)
{
sum+=(abs(j-i)+abs(i-)+abs(j-))**a[j];//各次上下楼层消耗电量的和
}
ans=min(sum,ans);//找出最优的答案
}
printf("%ld\n",ans);
return ;
}
感觉好水。。。
电梯问题——致敬ACM的更多相关文章
- 楼天城楼教主的acm心路历程(作为励志用)
楼主个人博客:小杰博客 利用假期空暇之时,将这几年GCJ,ACM,TopCoder 參加的一些重要比赛作个 回顾.昨天是GCJ2006 的回顾,今天时间上更早一些吧,我如今还清晰记得3 年 前,我刚刚 ...
- 【转】楼天城楼教主的acm心路历程(作为励志用)
利用假期空闲之时,将这几年GCJ,ACM,TopCoder 参加的一些重要比赛作个回顾.昨天是GCJ2006 的回忆,今天时间上更早一些吧,我现在还清晰记得3 年前,我刚刚参加ACM 时参加北京赛区2 ...
- 剪辑的楼天城的ACM之路
楼天城楼教主的acm心路历程(剪辑) 利用假期空闲之时,将这几年GCJ,ACM,TopCoder 参加的一些重要比赛作个回顾.昨天是GCJ2006 的回忆,今天时间上更早一些吧,我现在还清晰记得3 年 ...
- 楼塔当天领袖acm心理(作为励志使用)
楼主个人博客:吉尔博客 假期空闲的时候使用.这些年来GCJ.ACM,TopCoder 的一个号码的一重要的比赛的参与 回顾.GCJ2006 的回顾,今天时间上更早一些吧,我如今还清晰记得3 年 前.我 ...
- 2013腾讯编程马拉松||HDU 4505 小Q系列故事——电梯里的爱情 水水水
http://acm.hdu.edu.cn/showproblem.php?pid=4505 题目大意: 电梯最开始在0层,并且最后必须再回到0层才算一趟任务结束.假设在开始的时候已知电梯内的每个人要 ...
- SCNU ACM 2016新生赛决赛 解题报告
新生初赛题目.解题思路.参考代码一览 A. 拒绝虐狗 Problem Description CZJ 去排队打饭的时候看到前面有几对情侣秀恩爱,作为单身狗的 CZJ 表示很难受. 现在给出一个字符串代 ...
- SCNU ACM 2016新生赛初赛 解题报告
新生初赛题目.解题思路.参考代码一览 1001. 无聊的日常 Problem Description 两位小朋友小A和小B无聊时玩了个游戏,在限定时间内说出一排数字,那边说出的数大就赢,你的工作是帮他 ...
- acm结束了
最后一场比赛打完了.之前为了记录一些题目,开了这个博客,现在结束了acm,这个博客之后也不再更新了. 大家继续加油!
- 关于ACM的总结
看了不少大神的退役帖,今天终于要本弱装一波逼祭奠一下我关于ACM的回忆. 从大二上开始接触到大三下结束,接近两年的时间,对于大神们来说两年的确算不上时间,然而对于本弱来说就是大学的一半时光.大一的懵懂 ...
随机推荐
- D9 图论综合题
1.白银莲花池 LUOGU 2411 第一种思路:当然我们可以写三个bfs a掉这个题,这写下来一二百行要有了吧: 第二种:我们可以在一个bfs中维护所有的信息,一个方向数组,从起点开始,向八个方向扩 ...
- iOS日期问题
由于项目需要,需要获取去设备的当前时间,组成一个字符串,比如 2018年9月15日 15点30分30秒,需要转换成字符创:180915153030. 很简单的一个需求,于是就使用了日期格式话当前时间: ...
- 关于C#的Main(String[] args)参数输入问题
关于C#的Main(String[] args)参数输入问题 指定相关的测试代码 首先,写一个用于测试的关于Main(String[] args)参数输入有关的代码类,如下: using System ...
- Polly 熔断策略
熔断策略主要以 CircuitBreaker 来完成. 工作原理 熔断器可以被看作为一个主要含有三个状态的状态机 如果以电路开关来看: 开关闭合对应 CLOSED 状态, 开关打开对应 OPEN 状态 ...
- error_Could not load file or assembly
原文链接 Could you be missing the loaded assembly from your configuration file? Ensure you have somethin ...
- Spring之bean的生命周期
这篇博文是spring生命周期的详解,目前限于作者自身的水平对于一些内容可能只知其然不知其所以然,所以博文中如果出现错误欢迎各位指出,同时我也会逐步提升自己的水平,争取能够多发布一些能让大家获益的博文 ...
- SAP ERP SD模块中维护销售人员
SAP ERP SD模块中维护销售人员信息并分配销售组织 分类: SAPHCM用户指南 在SAP ERP系统,销售和分销(SD)模块中需要创建销售人员(Sales Personnels)消息, ...
- [MySQL] timestamp和datetime的区别
建表语句如下: create table strong_passwd_whitelist( id int unsigned not null auto_increment, email_id int ...
- Unity日常记录 - QualitySettings 性能设置
unity打包时,可通过QualitySettings优化图像性能,这是最常设置也是最明显的图像性能体现 设置图形质量的水平,一般来说,质量是以牺牲性能为代价的,所以最好不要追求移动设备或旧硬件的最高 ...
- loadrunner中JavaVuser脚本的编写
1.环境准备: LoadRunner11----->对应JDK1.6版本(32位) LoadRunner12----->对应JDK1.7版本(32位) (一).JDK下载安装完成 ...