【CODEFORCES】 B. Design Tutorial: Learn from Life
2015-07-09 11:31
736 查看
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
output
input
output
input
output
Note
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.
这题千万不要被Note误导了,不要从小到大,从大到小就好,代码就是分分钟的事....
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:
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.
这题千万不要被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; }
相关文章推荐
- js如何弹出新窗口
- liferay修改、去掉右上角控制面板等源码路径
- caffe layer层详解
- JSTL(JSP Standard Tag Library,JSP标准标签库)
- Populating Next Right Pointers in Each Node
- JSR303 Bean Validation 技术规范特性概述
- 前端小tips之标签
- js拼接json格式数据
- JS原生函数总结
- 判断jQuery对象或dom对象是否存在
- jQuery选择器
- JSON与XML的区别比较(转载)
- JavaScript学习笔记(一)
- JSTL开发实例详解
- 如何去设计一个自适应的网页设计或HTMl5
- HTML 5 拖放
- jQuery ui datepicker 日历转中文
- firebug插件-如何使用firefox进行网页js调试
- Stack Overflow requires external JavaScript from another domain, which is blocked or failed to load.
- jsp上的radio