【CODEFORCES】 B. Design Tutorial: Learn from Life

原创 2015年07月09日 11:31:27

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 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.

这题千万不要被Note误导了,不要从小到大,从大到小就好,代码就是分分钟的事....

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int n,k,m,a[2005];

void qsort(int a[],int low,int high)
{
    int i=low,j=high,t=a[(low+high)/2];
    while  (i<j)
    {
        while (a[i]<t) i++;
        while (a[j]>t) j--;
        if (i<=j)
        {
            int k;
            k=a[i]; a[i]=a[j]; a[j]=k;
            i++; j--;
        }
    }
    if (i<high) qsort(a,i,high);
    if (j>low) qsort(a,low,j);
}


int main()
{
    scanf("%d%d",&n,&k);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    qsort(a,1,n);
    int i,v,p,ans=0;
    i=n;
    while (i>=1)
    {
        ans+=(a[i]-1)*2;
        v=k; p=a[i];
        while (i>=1 && v>0)
        {
            i--;
            v--;
        }
    }
    cout <<ans<<endl;
    return 0;
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

【codeforces】 472C Design Tutorial: Make It Nondeterministic

简单贪心。。。。

【codeforces】 472D Design Tutorial: Inverse the Problem

Let's think it in the following way: for the minimal length edge, it must belong the the tree, ..., ...

Codeforces 472D Design Tutorial: Inverse the Problem【MST+SPFA+思维】

D. Design Tutorial: Inverse the Problem time limit per test 2 seconds memory limit per test ...

Codeforces Round #270——C. Design Tutorial: Make It Nondeterministic

C. Design Tutorial: Make It Nondeterministic time limit per test 2 seconds memory limit per t...

Codeforces 472C Design Tutorial: Make It Nondeterministic【贪心】

C. Design Tutorial: Make It Nondeterministic time limit per test 2 seconds memory limit per t...

codeforces472D Design Tutorial: Inverse the Problem 最小生成树+dfs

题意:

Codeforces Round #270 G. Design Tutorial: Increase the Constraints 分块+FFT

我们如果要求两个串的哈密顿距离我们可以这样把第二个串反向,然后字母为1的地方系数为1,字母为0的系数为-1,所以我们可以算出x-y=a[n-1](x是相同的点数,y是不同的点数)有因为x+y=n所以y...

【CODEFORCES】 C. Design Tutorial: Make It Nondeterministic

C. Design Tutorial: Make It Nondeterministic time limit per test 2 seconds memory limit p...
  • S_Black
  • S_Black
  • 2015年07月09日 11:34
  • 548

Codeforces 472C - Design Tutorial: Make It Nondeterministic(模拟)

题意 每个人可以选择first name 或者 last name 当自己的账号,现在给出最终的字典序排位,问能不能实现 思路 模拟一下。 lst用于记录上一个人选择的...

Codeforces 472D. Design Tutorial: Inverse the Problem

D. Design Tutorial: Inverse the Problem time limit per test2 seconds memory limit per test256 mega...
  • nare123
  • nare123
  • 2016年08月16日 08:39
  • 284
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【CODEFORCES】 B. Design Tutorial: Learn from Life
举报原因:
原因补充:

(最多只允许输入30个字)