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 ...
随机推荐
- Android自动化测试基础知识——MONKEY测试工具(转的)
本周开始启动手机输入法simeiji的自动化测试,同时开始接触手机浏览器自动化测试.接下来会对android自动化测试工具和方法做一个专题研究. 第一篇介绍monkey测试工具. 1 自动化测试背景 ...
- Xcode 静态库调试策略
Xcode 静态库调试策略 (已经有现成的工程和静态库源码) ***** 为安全期间建议备份一下静态库 Step1: 下载最新的工程[工程中有所要测试的静态库和头文件需要删除]: Step2: ...
- 永久性for循环配合switch语句可以实现菜单功能
永久性for循环配合switch语句可以实现菜单功能总结:加入想要无条件地跳转到某条语句执行,用goto语句: 加入想要对某种条件进行判断,为真或为假分别执行不同的语句,用if语句 加入想要检测的条件 ...
- [转] iOS (OC) 中 KVC 与 KVO 理解
转自: http://magicalboy.com/kvc_and_kvo/ KVC 与 KVO 是 Objective C 的关键概念,个人认为必须理解的东西,下面是实例讲解. Key-Value ...
- postgresql 忘记 postgres 密码
参考博文: 如何重置postgresql用户密码 解决方法: 1.关闭数据库服务 2.进入数据库的工作空间目录 (如果是建库是没有另外指定,应该就是postgresql安装目录下的 data 目录) ...
- Python监控网站运行状况
利用python便捷的类库,可以方便快速实现对网站运行状况的监控,主要包括对80端口(即网站运行端口),其它tcp服务等端口的监控就可以了解服务器大概的一个运行状况,使用的库主要为urllib2及so ...
- 使用Qt设计师文件的3种方式
使用Qt设计师设计的.ui界面文件是XML文件,有3种方式在PyQt中使用,本文将通过实例进行讲解. 使用PyQt中如何结合Qt设计师进行开发中的例子.点击按钮修改标签的内容. 1. 直接使用ui文件 ...
- 省份、城市、区县三级联动Html代码
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- ASP.NET MVC 5 学习教程:添加模型
原文 ASP.NET MVC 5 学习教程:添加模型 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 通过控 ...
- Python 2.7 学习笔记 模块和包
我们来考虑下如下几种场景: 1.编写一个python程序,如果程序比较简单,则可以把代码放到一个python文件中.但如果程序功能比较多,可能需要多个python文件来组织源代码.而这些文件之间的代码 ...