codeforces 472BDesign Tutorial: Learn from Life(简单贪心)

来源:互联网 发布:2017网络教育报名时间 编辑:程序博客网 时间:2024/06/12 00:18
B. Design Tutorial: Learn from Life
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 22 3 4
output
8
input
4 250 100 50 100
output
296
input
10 32 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,问把这些人送到指定楼层所花的最少的时间,最终电梯需要回到一楼。电梯刚开始停留在一楼
思路:这题一看就知道是用贪心来写,然后  打开我们的脑洞想一下,想到先把最高层的人送上去,然后花在下来的时间也仅仅是最高层的一次时间,夸张一点,比如给你 1 2 99 100 然后电梯容量为2个,这样,直接把100层和99层的放进去,这样下来只要一百层,而不需要再上到99层再下来,这样可以节省很多时间,所以这样会使最优解

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int main(){    int n,k;    int num[2100];    while (~scanf("%d%d",&n,&k ) )    {        for (int i = 0 ; i < n ; i ++ )        {            scanf("%d",&num[i]);        }        sort(num,num+n);        //排序        int res = 0;        for(int i=n-1; i>=0; i-=k)        {               res += 2*(num[i] - 1);      //上下合起来 所以乘2 只需要k个人中的最大层数就可以        }        printf("%d\n", res);    }}


0 0