ZSoft Corp. is a software company in GaoKe Hall. And the workers in the hall are very hard-working. But the elevator in that hall always drives them crazy. Why? Because there is only one elevator in GaoKe Hall, while there are hundreds of companies in it. Every morning, people must waste a lot of time waiting for the elevator.

Hal, a smart guy in ZSoft, wants to change this situation. He wants to find a way to make the elevator work more effectively. But it's not an easy job.

There are 31 floors in GaoKe Hall. It takes 4 seconds for the elevator to raise one floor. It means:

It costs (31 - 1)×4 = 120 seconds if the elevator goes from the 1-st floor to the 31-st floor without stop. And the elevator stops 10 second once. So, if the elevator stops at each floor, it will cost 30×4 + 29×10 = 410 seconds (It is not necessary to calculate the stopping time at 31st floor). In another way, it takes 20 seconds for the workers to go up or down one floor. It takes 30×20 = 600 seconds for them to walk from the 1-st floor to the 31-st floor. Obviously, it is not a good idea. So some people choose to use the elevator to get a floor which is the nearest to their office.

After thinking over for a long time, Hal finally found a way to improve this situation. He told the elevator man his idea: First, the elevator man asks the people which floors they want to go. He will then design a stopping plan which minimize the time the last person need to arrive the floor where his office locates. For example, if the elevator is required to stop at the 4-th, 5-th and 10-th floor, the stopping plan would be: the elevator stops at 4-th and 10-th floor. Because the elevator will arrive 4th floor at 3×4 = 12 second, then it will stop 10 seconds, then it will arrive 10th floor at 3×4 + 10 + 6×4 = 46 second. People who want to go 4-th floor will reach their office at 12 second, people who want to go to 5-th floor will reach at 12 + 20 = 32 second and people who want to go to 10-th floor will reach at 46 second. Therefore it takes 46 seconds for the last person to reach his office. It is a good deal for all people.

Now, you are supposed to write a program to help the elevator man to design the stopping plan, which minimize the time the last person needs to arrive at his floor.

Input

The input consists of several testcases. Each testcase is in a single line as the following:

n f
1 
f
2 ... 
f
n

It means, there are totally n floors at which the elevator need to stop, and n = 0 means no testcases any more. f1 f2 ... fn are the floors at which the elevator is to be stopped (n30, 2f1 < f2 < ... < fn31). Every number is separated by a single space.

Output

For each testcase, output the time the last reading person needs in the first line and the stopping floors in the second line. Please note that there is a summary of the floors at the head of the second line. There may be several solutions, any appropriate one is accepted. No extra spaces are allowed.

Sample Input

3 4 5 10
1 2
0

Sample Output

46
2 4 10
4
1 2

题意:一个公司只有一台电梯,现在有n个人,他们分别有一个想到的楼层,如果坐电梯4秒一层,但是停电梯要花费10秒,如果走路要20秒一层。问如何保证最后一个人到达目的楼层时间最小。

思路:贪心 + 二分,二分时间,知道一个最小的时间满足所有人都能到相应楼层。在二分的过程中利用贪心来判断每个人是否能到相应楼层。我们知道在限定时间内,如果电梯在那个人的当前楼层停下,那个人所要花费的时间是最短的。如果不能满足这个条件,这个限定时间即为不合理。如果能满足条件,我们在考虑,尽量让电梯往高了去开门,因为这样的话,能满足更多人的需求。因为往高了去开门在走下楼,如果当前这个人满足了。之后几个层数的人时间肯定不会比这个人大,肯定都是满足的。这样可以在限定时间内让更多人到达相应楼层。利用这样的策略,求得最终解。并且保存下开门的层数以便输出。

代码:

#include <stdio.h>
#include <string.h> int n, vis[35], i, sb, top, start, end, mid, open[35], num; void init() {
top = 1;
memset(vis, 0, sizeof(vis));
for (i = 0; i < n; i ++) {
scanf("%d", &sb);
vis[sb] = 1;
if (sb > top)
top = sb;
}
start = 0;
end = 14 * (top - 1);
} int judge(int t) {
int i, j;
num = 0;
i = t / 20 + 2;
while (i <= top) {
while (i <= top && !vis[i])
i ++;
if (4 * (i - 1) + 10 * num > t)
return 0;
j = (t - 10 * num + 20 * i + 4) / 24;
i = (t - 10 * num + 16 * j + 4) / 20 + 1;
open[num ++] = j;
}
return 1;
} int solve() {
while (start < end - 1) {
mid = (start + end) / 2;
if (judge(mid))
end = mid;
else
start = mid;
}
return end;
}
int main() {
while (~scanf("%d", &n) && n) {
init();
printf("%d\n", solve());
judge(end);
printf("%d", num);
for (i = 0; i < num; i ++)
printf(" %d", open[i]);
printf("\n");
}
return 0;
}

UVALive 2949 Elevator Stopping Plan(二分 + 贪心)的更多相关文章

  1. Gym 101194D / UVALive 7900 - Ice Cream Tower - [二分+贪心][2016 EC-Final Problem D]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

  2. UVaLive 3971 Assemble (水题二分+贪心)

    题意:你有b元钱,有n个配件,每个配件有各类,品质因子,价格,要每种买一个,让最差的品质因子尽量大. 析:很简单的一个二分题,二分品质因子即可,每次计算要花的钱的多少,每次尽量买便宜且大的品质因子. ...

  3. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  4. 【CF526G】Spiders Evil Plan(贪心)

    [CF526G]Spiders Evil Plan(贪心) 题面 洛谷 CodeForces 给定一棵树,要求选择\(y\)条链,满足被链覆盖的所有点在树上联通,且\(x\)必定在联通块中. 对于每次 ...

  5. 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心

    /** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...

  6. 【bzoj2097】[Usaco2010 Dec]Exercise 奶牛健美操 二分+贪心

    题目描述 Farmer John为了保持奶牛们的健康,让可怜的奶牛们不停在牧场之间 的小路上奔跑.这些奶牛的路径集合可以被表示成一个点集和一些连接 两个顶点的双向路,使得每对点之间恰好有一条简单路径. ...

  7. Codeforces_732D_(二分贪心)

    D. Exams time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  8. CF732D Exams 二分 贪心

    思路:二分+贪心 提交次数:10次以上 错因:刚开始以为二分(边界,$+1or-1$)写错了,调了半天,后来才发现是$ck()$写错了.开始只判了最后是否小于零,而应该中间一旦小于零就$return\ ...

  9. $CF949D\ Curfew$ 二分/贪心

    正解:二分/贪心 解题报告: 传送门$QwQ$ 首先这里是二分还是蛮显然的?考虑二分那个最大值,然后先保证一个老师是合法的再看另一个老师那里是否合法就成$QwQ$. 发现不太会搞这个合不合法的所以咕了 ...

随机推荐

  1. php在linux后台执行

    <?php ignore_user_abort();//后台运行 ini_set('default_socket_timeout', -1);//socket不超时 set_time_limit ...

  2. 封装scroll.js 获取滚动条的值

    function Obj(){} Obj.prototype={ scroll:function(){ /* 主要是做兼容处理 这里必须时!=null 因为默认值和每次滚动的时侯 都可以值为0 但是 ...

  3. flash从数据流判断图片格式防止xss攻击

    前段时间测试人员报了一个flash的xss bug,经分析用了Loader.loadBytes且没做数据流格式校验的程序都会中招,自测方法只需一行代码: ExternalInterface.call( ...

  4. IOS7开发~Images.xcassets

    from:http://blog.csdn.net/liufan321/article/details/9121241 新建项目,如下所示: 本文分享一下Images.xcassets的体验~_~ 1 ...

  5. 使用CSS3的@media来实现网页自适应

    如今,电脑显示器的屏幕分辨率向越来越大发展,而手机等移动设备终端的分辨率却不可能大到哪里去.越来越多的网站,开始让自己的页面自适合各种分辨率,在小分辨率下显示基本的内容,在大分辨率下显示全部功能,甚至 ...

  6. 如何在IE11中开启WebGL暨微软和WebGL的恩怨情仇录

    正如我们上周报道的,国外开发者Francois Remy在泄露版Windows Blue附带的Internet Explorer 11中发现,WebGL接口已经封装完成,但功能上还未能开放支持.在这之 ...

  7. 22LINQ查询运算符返回IEnumerable<T>实例汇总

    本篇体验LINQ的各种查询运算符.   先创建一个泛型方法,用来显示查询结果: private static void DisplayQuery<T>(IEnumerable<T&g ...

  8. Windows下编译memcached-1.4.5(32bit和64bit)

    1.简介 Memcached 是一个高性能的分布式内存对象缓存系统.它通过将数据缓存在内存中来减少对数据库和文件系统的访问,减轻数据库及操作系统的负担,提高应用系统的速度. 目前已经很多系统应用了me ...

  9. Informatica 常用组件Aggregator之一 聚合表达式

    转换类型:已连接.主动        聚合转换允许您执行聚合计算,比如平均值和总和.聚合转换与表达式转换不同,您可以使用聚合转换对多组执行计算.而表达式转换只允许您逐行地执行计算.        使用 ...

  10. C#版查杀本地/远程进程工具

    xkill [原创] Author: R&S E-mail: yrwithsh@vip.sina.com HomePage: fz5fz.yeah.net Date: 10/04/2003 u ...