codeforces472B

本文介绍了一个基于实际生活场景的算法问题——电梯调度。该问题通过模拟多人等待并使用电梯到达不同楼层的情景,提出了一种贪心算法解决方案。该算法首先将乘客目标楼层进行排序,然后从最高楼层开始逐层向下运送乘客,每次运送不超过电梯的最大容量,以达到最小化总运行时间的目的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Design Tutorial: Learn from Life

 CodeForces - 472B 

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.

Examples

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

 

sol:一种较为显然的贪心就是按楼层排序后从高到低从客人,应该是最优的了

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0'); return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=2005;
int n,K,A[N];
int main()
{
    int i,ans=0;
    R(n); R(K);
    for(i=1;i<=n;i++) R(A[i]);
    sort(A+1,A+n+1);
    for(i=n;i>=1;i-=K)
    {
        int j=max(1,i-K+1);
        ans+=(A[i]-1)*2;
    }
    Wl(ans);
    return 0;
}
/*
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
*/
View Code

 

转载于:https://www.cnblogs.com/gaojunonly1/p/10585175.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值