Codeforces Round #270--B. Design Tutorial: Learn from Life
1 second
256 megabytes
standard input
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?
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.
题目大意:一楼有n个人等电梯。电梯的容量为k,。怎样用最少的时间把全部人都送到要去的楼层。当中楼梯的初、末位置均为1楼。
解题思路:贪心。这个贪心确实不太好想,智商不够。搞了好几天。先按楼层从小到大排序。最优的解是,先把电梯n%k个人放到他们要去的楼层。这是就会把k - (n%k)个人带到a[n%k]层。然后电梯再下去接k个人,途中下一部分。然后继续把原来那k - (n%k)个人带着继续上升,一直反复这样,就会刚好把全部人送到他们要去的楼层,而且保证是最优解。
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[2005]; int main(){
// freopen("in.txt","r",stdin);
int n, k;
while(scanf("%d%d", &n, &k) == 2){
for(int i=0; i<n; i++){
scanf("%d", &a[i]);
}
sort(a, a+n);
int ans = 0;
for(int i=n-1; i>=0; i-=k)
ans += 2*(a[i] - 1);
printf("%d\n", ans);
}
return 0;
}
Codeforces Round #270--B. Design Tutorial: Learn from Life的更多相关文章
- codeforces水题100道 第七题 Codeforces Round #270 A. Design Tutorial: Learn from Math (math)
题目链接:http://www.codeforces.com/problemset/problem/472/A题意:给你一个数n,将n表示为两个合数(即非素数)的和.C++代码: #include & ...
- Codeforces Round #270 A. Design Tutorial: Learn from Math【数论/埃氏筛法】
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Codeforces Round #270 D Design Tutorial: Inverse the Problem --MST + DFS
题意:给出一个距离矩阵,问是不是一颗正确的带权树. 解法:先按找距离矩阵建一颗最小生成树,因为给出的距离都是最短的点间距离,然后再对每个点跑dfs得出应该的dis[][],再对比dis和原来的mp是否 ...
- Codeforces Round #270 A~D
Codeforces Round #270 A. Design Tutorial: Learn from Math time limit per test 1 second memory limit ...
- Codeforces Round #270 1002
Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...
- Codeforces Round #270 1001
Codeforces Round #270 1001 A. Design Tutorial: Learn from Math time limit per test 1 second memory l ...
- Codeforces Round #270 1003
Codeforces Round #270 1003 C. Design Tutorial: Make It Nondeterministic time limit per test 2 second ...
- Design Tutorial: Learn from Life
Codeforces Round #270 B:http://codeforces.com/contest/472/problem/B 题意:n个人在1楼,想要做电梯上楼,只有1个电梯,每次只能运k个 ...
- cf472B Design Tutorial: Learn from Life
B. Design Tutorial: Learn from Life time limit per test 1 second memory limit per test 256 megabytes ...
随机推荐
- 【JQuery】eval()出现missing after property id 错误。
是因为数据没有转换成json格式输出就直接eval了. 正确步骤:后台: JsonBinder.buildNormalBinder().toJson(list); 前台: eval('(${posit ...
- iOS系统自带的 UIAlertView 自动旋转的实现
这里主要解析 UIAlertView 的几个关键功能的实现: 随着设备屏幕的旋转而旋转: Alert弹出框,使用UIWindow来实现,就是说,不用依赖于当前显示在最前面的UIView. 实现源码参考 ...
- C#高级编程随笔
1.把类创作的变量叫做对象2.类就是对象的模版3.类定义了每个对象的数据和功能4.接口不能被实例化,抽象类不能被实例化5.抽象基类可以包含非抽象方法,而接口只能包含抽象方法6.一个类可以实现多个接口7 ...
- 内核必看: spinlock、 mutex 以及 semaphore
linux 内核的几种锁介绍 http://wenku.baidu.com/link?url=RdvuOpN3RPiC5aY0fKi2Xqw2MyTnpZwZbE07JriN7raJ_L6Ss8Ru1 ...
- [转]Centos6.5安装配置keepalived
参考博文: Centos6.5安装配置keepalived CentOS6.5 keepalived详解及实现Nginx服务的高可用性 CentOS6.5 LVS + KeepAlived搭建步骤 我 ...
- [转]如何从MySQL官方Yum仓库安装MySQL5.6
参考博文: 如何从MySQL官方Yum仓库安装MySQL5.6 Centos 升级Mysql版本或者Yum安装Mysql5.6 2013年10月,MySQL开发团队正式宣布支持Yum仓库,这就意味着我 ...
- C语言深度剖析--volatile(转载)
volatile关键字和const一样是一种类型修饰符,用它修饰的变量表示可以被某些编译器未知的因素更改,比如操作系统,硬件或者其他线程等等.遇到这个关键字声明的变量,编译器对访问该变量的代码就不再进 ...
- django-extensions
命令行: admin后台管理扩展 后面会出现个放大镜实现搜索补齐功能. 交互式的 Python Shells(shell_plus) 实现自动导入 如果遇到apps中包含的的models名字出现冲突, ...
- Ruby学习-第一章
第一章 字符串,数字,类和对象 为了证明Ruby真的好用,hello world也能写的如此简洁: puts 'hello world' 1.输入/输出 print('Enter your name' ...
- ACM比赛(第二次A)
ime Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Description There is ...