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?
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 a single integer — the minimal time needed to achieve the goal.
3 2 2 3 4
8
4 2 50 100 50 100
296
10 3 2 2 2 2 2 2 2 2 2 2
8
In first sample, an optimal solution is:
- The elevator takes up person #1 and person #2.
- It goes to the 2nd floor.
- Both people go out of the elevator.
- The elevator goes back to the 1st floor.
- Then the elevator takes up person #3.
- And it goes to the 2nd floor.
- It picks up person #2.
- Then it goes to the 3rd floor.
- Person #2 goes out.
- Then it goes to the 4th floor, where person #3 goes out.
- The elevator goes back to the 1st floor.
不知道为什么,就觉得这题很扯
坐电梯问题,问最小时间。关键是这题给了提示,提示可以脑补出答案,排个序后模拟下可以了。
但,如果不给提示呢?能做出来不?所以记下这道题。
//Hello. I'm Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-6
#define MOD 1000000007
#define MAXN 1001000
#define N 5000
#define M
int n,k,maxi,mini;
int f[N];
int main()
{
cin>>n>>k;
maxi=0;
repin(i,1,n)
{
scanf("%d",f+i);
maxi=max(maxi,f[i]);
}
sort(f+1,f+1+n);
int num=n,ans=0,tail=1,start=1,numleft,tt,to;
while(num>0)
{
if(num<=k)
{
ans+=maxi-start;
num=0;
continue;
}
numleft=num-k;
to=n-numleft;
mini=f[tail];
ans+=mini-start;
while(f[tail]==mini && tail<=to)
{
num--;
tail++;
}
if(num<=0) break;
tt=numleft/k;
if(numleft%k!=0) tt++;
ans+=2*tt*(mini-start);
start=mini;
}
ans+=maxi-1;
printf("%d\n",ans);
}