time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

One way to create a task is to learn from life. You can choose some experience in real life, formalize it and then you will get a new task.

Let's think about a scene in real life: there are lots of people waiting in front of the elevator, each person wants to go to a certain floor. We can formalize it in the following way. We have n people
standing on the first floor, the i-th person wants to go to the fi-th
floor. Unfortunately, there is only one elevator and its capacity equal to k (that is at most k people
can use it simultaneously). Initially the elevator is located on the first floor. The elevator needs |a - b| seconds to move from the a-th
floor to the b-th floor (we don't count the time the people need to get on and off the elevator).

What is the minimal number of seconds that is needed to transport all the people to the corresponding floors and then return the elevator to the first floor?

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 2000) —
the number of people and the maximal capacity of the elevator.

The next line contains n integers: f1, f2, ..., fn (2 ≤ fi ≤ 2000),
where fi denotes
the target floor of the i-th person.

Output

Output a single integer — the minimal time needed to achieve the goal.

Sample test(s)
input
3 2
2 3 4
output
8
input
4 2
50 100 50 100
output
296
input
10 3
2 2 2 2 2 2 2 2 2 2
output
8
Note

In first sample, an optimal solution is:

  1. The elevator takes up person #1 and person #2.
  2. It goes to the 2nd floor.
  3. Both people go out of the elevator.
  4. The elevator goes back to the 1st floor.
  5. Then the elevator takes up person #3.
  6. And it goes to the 2nd floor.
  7. It picks up person #2.
  8. Then it goes to the 3rd floor.
  9. Person #2 goes out.
  10. Then it goes to the 4th floor, where person #3 goes out.
  11. The elevator goes back to the 1st floor.

题目大意:一楼有n个人等电梯。电梯的容量为k,。怎样用最少的时间把全部人都送到要去的楼层。当中楼梯的初、末位置均为1楼。

解题思路:贪心。这个贪心确实不太好想,智商不够。搞了好几天。先按楼层从小到大排序。最优的解是,先把电梯n%k个人放到他们要去的楼层。这是就会把k - (n%k)个人带到a[n%k]层。然后电梯再下去接k个人,途中下一部分。然后继续把原来那k - (n%k)个人带着继续上升,一直反复这样,就会刚好把全部人送到他们要去的楼层,而且保证是最优解。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[2005]; int main(){
// freopen("in.txt","r",stdin);
int n, k;
while(scanf("%d%d", &n, &k) == 2){
for(int i=0; i<n; i++){
scanf("%d", &a[i]);
}
sort(a, a+n);
int ans = 0;
for(int i=n-1; i>=0; i-=k)
ans += 2*(a[i] - 1);
printf("%d\n", ans);
}
return 0;
}

Codeforces Round #270--B. Design Tutorial: Learn from Life的更多相关文章

  1. codeforces水题100道 第七题 Codeforces Round #270 A. Design Tutorial: Learn from Math (math)

    题目链接:http://www.codeforces.com/problemset/problem/472/A题意:给你一个数n,将n表示为两个合数(即非素数)的和.C++代码: #include & ...

  2. Codeforces Round #270 A. Design Tutorial: Learn from Math【数论/埃氏筛法】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  3. Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS

    题意:给出一个距离矩阵,问是不是一颗正确的带权树. 解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑dfs得出应该的dis[][],再对比dis和原来的mp是否 ...

  4. Codeforces Round #270 A~D

    Codeforces Round #270 A. Design Tutorial: Learn from Math time limit per test 1 second memory limit ...

  5. Codeforces Round #270 1002

    Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...

  6. Codeforces Round #270 1001

    Codeforces Round #270 1001 A. Design Tutorial: Learn from Math time limit per test 1 second memory l ...

  7. Codeforces Round #270 1003

    Codeforces Round #270 1003 C. Design Tutorial: Make It Nondeterministic time limit per test 2 second ...

  8. Design Tutorial: Learn from Life

    Codeforces Round #270 B:http://codeforces.com/contest/472/problem/B 题意:n个人在1楼,想要做电梯上楼,只有1个电梯,每次只能运k个 ...

  9. cf472B Design Tutorial: Learn from Life

    B. Design Tutorial: Learn from Life time limit per test 1 second memory limit per test 256 megabytes ...

随机推荐

  1. TCP/IP之三次握手、四次挥手

    参照:http://www.cnblogs.com/hnrainll/archive/2011/10/14/2212415.html 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建 ...

  2. stm32之595(spi芯片)

    595是一款串转并的芯片:  (三极管的功能) /*Include---------------------------*/ #include"stm32f10x_lib.h" / ...

  3. 【转】理解RESTful架构

    [转]理解RESTful架构 越来越多的人开始意识到,网站即软件,而且是一种新型的软件. 这种"互联网软件"采用客户端/服务器模式,建立在分布式体系上,通过互联网通信,具有高延时( ...

  4. ThinkPHP - 自动创建 + 自动验证 + 自动完成

    自动创建:创建数据模型. $User->create(); 自动验证:验证提交的表单数据. protected $_validate = array( array('verify','requi ...

  5. Codeforces Round #316 (Div. 2A) 570A Elections

    题目:Click here #include <bits/stdc++.h> using namespace std; typedef long long ll; const int IN ...

  6. 性能测试工具:AB

    ###################################################################################格式:ab -n 请求次数 -c ...

  7. 基于visual Studio2013解决算法导论之028散列表开放寻址

     题目 散列表 解决代码及点评 #include <iostream> #include <time.h> using namespace std; template & ...

  8. 《UNIX环境高级编程》笔记--sync、fsync和fdatasync函数

    传统的UNIX实现在内核中设有缓冲区高速缓存或页面高速缓存,大多数磁盘 I/O都通过缓冲进行.当将数据写入文件时,内核通常先将该数据复制到其中一个缓冲区中,如果该缓冲区尚未写满,则并不将其排入输出队列 ...

  9. Android 电话自己主动接听和挂断具体解释

    1.通过aidl及反射实现挂断电话 详细分三步: (1)ITelephony.aidl ,必须新建com.android.internal.telephony包并放入ITelephony.aidl文件 ...

  10. HDU 2040:亲和数

    亲和数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...