codeforces472B
Design Tutorial: Learn from Life
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
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
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
sol:一种较为显然的贪心就是按楼层排序后从高到低从客人,应该是最优的了
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,K,A[N];
int main()
{
int i,ans=;
R(n); R(K);
for(i=;i<=n;i++) R(A[i]);
sort(A+,A+n+);
for(i=n;i>=;i-=K)
{
int j=max(,i-K+);
ans+=(A[i]-)*;
}
Wl(ans);
return ;
}
/*
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
*/
codeforces472B的更多相关文章
随机推荐
- Photoshop 基础一 安装
安装 版本介绍 学习教程 一.安装 1)注册Adobe账号,注册地址:Adobe注册 2)下载地址:Adobe下载 下载地址2:百度经验 3)安装:试用期7天的版本 二.版本介绍 1)最新版本:A ...
- 一、java虚拟机内存区域
内存区域 java虚拟机在java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.java虚拟机规范将JVM管理的内存分为:程序计数器.本地方法栈.Java虚拟机栈.方法区.Java堆.如下 ...
- Lua 中的条件表达式
下面这代码段看上去很熟悉,就是C#里面的条件表达式,很多其它语言也都有这么一个条件表达式. ; ; string c = "c"; string d = "d" ...
- Spring 面试问题 TOP 50
Spring 面试问题 TOP 50 Spring Framework 现在几乎已成为 Java Web 开发的标配框架.那么,作为 Java 程序员,你对 Spring 的主要技术点又掌握了多少呢? ...
- BZOJ4614/UVA1742 Oil 计算几何
传送门 题意:在平面直角坐标系中给出$N$条互不相交的.与$x$轴平行.且在$x$轴上方的线段,每一条线段的价值为其长度.求一条不与$x$轴平行的直线,使得与这条直线相交的线段的价值之和最大,求出这个 ...
- Adobe PhotoshopCC2017 安装与破解(Mac)
简单说明下Adobe Photoshop CC 2017的破解方法: 1.打开dmg镜像,双击“Install”进行安装,登陆Adobe ID(没有注册一个)完成安装: 2.解压缩“Adobe Zii ...
- decorator, async/await, generator
////////////decorator////////// function aopFunc (target, key, descriptor) { console.log('aopFunc') ...
- 历时25天,我的博客(www.ityouknow.com)终于又活了过来
时间回到2016年的7月10号,那时候我刚刚开始正式在博客园写博客,博客园的交流氛围很好,但鉴于博客园古老的界面,同时计划创建一个自己独立的博客,毕竟自己的博客怎么折腾都行. 那时候正在研究 Spri ...
- 批量实现多台服务器之间ssh无密码登录的相互信任关系
最近IDC上架了一批hadoop大数据业务服务器,由于集群环境需要在这些服务器之间实现ssh无密码登录的相互信任关系.具体的实现思路:在其中的任一台服务器上通过"ssh-keygen -t ...
- Python_函数的镶嵌和作用域链_26
def max(a,b): return a if a>b else b def the_max(x,y,z): #函数的嵌套调用 c = max(x,y) return max(c,z) pr ...