Time Limit: 1sec    Memory Limit:32MB 
Description
John is moving to the penthouse of a tall sky-scraper. He packed all his stuff in boxes and drove them to the entrance of the building on the ground floor. Unfortunately the elevator is out of order, so the boxes have to be moved up the stairs. 
Luckily John has a lot of friends that want to help carrying his boxes up. They all walk the stairway at the same speed of 1 floor per minute, regardless of whether they carry a box or not. The stairway however is so narrow that two persons can't pass each other on it. Therefore they deciced to do the following: someone with a box in his hands is always moving up and someone empty-handed is always moving down. When two persons meet each other somewhere on the stairway, the lower one (with a box) hands it over to the higher one (without a box). (And then the lower one walks down again and the higher one walks up.) The box exchange is instantaneous. When someone is back on the ground floor, he picks up a box and starts walking up. When someone is at the penthouse, he drops the box and walks down again.

After a while the persons are scattered across the stairway, some of them with boxes in their hands and some without. There are still a number of boxes on the ground floor and John is wondering how much more time it will take before all the boxes are up. Help him to find out!

Input
One line with a positive number: the number of test cases. Then for each test case:

  • One line with three numbers N, F, B with 1 ≤ N,F ≤ 1000 and 1 ≤ B ≤ 1000000: the number of persons, the number of floors (0=ground floor, F=penthouse) and the number of boxes that are still on the ground floor.
  • N lines with two numbers fi and bi with 0 ≤ fi ≤ F and bi = 0 or bi = 1: the floors where the persons are initially and whether or not they have a box in their hands (1=box, 0=no box).
Output
One line with the amount of time (in minutes) it will take to get all the remaining boxes to the penthouse. 
Sample Input
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAARABIDASIAAhEBAxEB/8QAGAABAAMBAAAAAAAAAAAAAAAAAAMFBwT/xAAlEAACAQQCAQMFAAAAAAAAAAABAgMABAURBiESIjFBMjZxdbP/xAAYAQACAwAAAAAAAAAAAAAAAAAAAwEEBf/EABsRAQEAAgMBAAAAAAAAAAAAAAEAAgMEEyFh/9oADAMBAAIRAxEAPwDQeRW+SyVnctBIkiiScOk87qm0ciP0aZWA8dkEDZA2fcGPCWPI+PXkUt3GIcQjkyQxTGdtMrAhUVQO5CraVd/UB1pa7cnHmbaW5hjxEktoZJJGulnjChWYsT4lvLoHvr3B1vommvuQYaSe/jGSxrW9yXEiCWIiTe9eWohvs/LH8n5ocDh9jlnsER+zt+9wDE9G0uKWO4hSaGRJIpFDI6MCrKewQR7ilVfFPs7B/r4P5rStB8ZJW9KUqIlKUoi//9k=" alt="" /> Copy sample input to clipboard 
2
3 10 5
0 0
0 0
0 0
2 5 1
2 1
3 0
Sample Output
30
8
首先这个可以看成一组人在移动,因为它的交换物品与方向跟没交换的效果是一样的。
另外考虑的就是,搬一个新的物品比已经在楼梯上的人来说它的速度总是更慢的。所以在搬运期间,还没到最后一趟的时候,大家搬运的个数是一样的,所以可以用 (boxNum / personNum - ) * 来计算在这期间的时间,剩下的就是初始状态在楼梯上和最后一趟的时间的考虑了。
#include <iostream>
#include <algorithm> using namespace std; int main(int argc, char const *argv[])
{
int testCase, personNum, floorNum, boxNum, floor, cally;
int person[];
cin >> testCase;
while (testCase--) {
cin >> personNum >> floorNum >> boxNum; for (int i = ; i != personNum; ++i) {
cin >> floor >> cally;
if (cally == )
person[i] = floorNum + floor;
else
person[i] = floorNum * - floor;
} sort(person, person + personNum); int remain = boxNum % personNum;
int minute = ;
if (remain == ) {
minute = (boxNum / personNum - ) * * floorNum + person[personNum - ]; // 上到顶端就不需要再下来了,所以 -1
} else {
minute = (boxNum / personNum) * * floorNum + person[remain - ]; // 上到顶端后还需要下来搬运剩下的
} cout << minute << endl;
}
return ;
}

sicily 1193. Up the Stairs的更多相关文章

  1. [LeetCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  2. [LintCode] Climbing Stairs 爬梯子问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  3. Leetcode: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

  4. sicily 中缀表达式转后缀表达式

    题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...

  5. sicily 1934. 移动小球

    Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...

  6. LintCode Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  7. 54. Search a 2D Matrix && Climbing Stairs (Easy)

    Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...

  8. Climbing Stairs

    Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...

  9. bzoj 1193 贪心

    如果两点的曼哈顿距离在一定范围内时我们直接暴力搜索就可以得到答案,那么开始贪心的跳,判断两点横纵坐标的差值,差值大的方向条2,小的条1,不断做,直到曼哈顿距离较小时可以暴力求解. 备注:开始想的是确定 ...

随机推荐

  1. BZOJ5072 小A的树(树形dp)

    容易猜到能选择的黑点个数是一个连续区间.那么设f[i][j]为i子树内选j个点形成包含根的连通块,最多有几个黑点,g[i][j]为最少有几个黑点,暴力dp是O(n2)的,求出每个连通块大小对应的黑点数 ...

  2. Contest 7

    A:搜索好难啊根本不会啊. B:原题都能写挂没救了啊.考虑求出每个数作为最小值时能向左向右扩展到的最远位置,那么这段区间里的所有数就不可能作为唯一的最小值成为最优解了,否则假设可以的话这段区间里的数都 ...

  3. 洛谷 P1560 蜗牛的旅行

    明显这是一道搜索题,其他题解写的有点复杂,我有更简便的写法 既然题目说走到不能再走,那我们就干脆一点,一条路走到黑,不到南墙不回头,一下把要走的路都走完,不但效率高,也好写,关键是大大节省了系统栈 一 ...

  4. Class类是什么? Class.forName()是干什么的?

    Class类概念 Class类用来描述一个类的结构,比如描述一个类有哪些成员,有哪些方法等.有多种方法可以获取一个类对应的Class类实例,比如: //第一种方式获取描述Dog类结构的Class类实例 ...

  5. Educational Codeforces Round 33 (Rated for Div. 2) 题解

    A.每个状态只有一种后续转移,判断每次转移是否都合法即可. #include <iostream> #include <cstdio> using namespace std; ...

  6. Eclipse在线安装spring-tool-suit插件

    查看eclipse版本:Help–>About Eclipse;如图1所示. 访问http://spring.io/tools/sts/all,复制在线安装url地址,不要下载ZIP文件,复制链 ...

  7. 【SPOJ】QTREE6(Link-Cut-Tree)

    [SPOJ]QTREE6(Link-Cut-Tree) 题面 Vjudge 题解 很神奇的一道题目 我们发现点有黑白两种,又是动态加边/删边 不难想到\(LCT\) 最爆力的做法,显然是每次修改单点颜 ...

  8. uoj 36 玛里苟斯

    [清华集训2014]玛里苟斯 - 题目 - Universal Online Judge k=1,2,3,4,5各占20pts是提示 应当分开考虑 k=1 拆位,如果第i位有1,则有1/2的概率xor ...

  9. 驱动之SPI,UART,I2C的介绍与应用20170118

    这篇文章主要介绍基本的驱动也是用的最多的协议类驱动中的SPI,I2C和UART.首先从最简单的UART也就是串口讲起: 1.UART UART由两根线也就是TX,RX以及波特率产生器组成,操作比较简单 ...

  10. SAS数据步与过程步,数据步语句

    SAS数据步与过程步,数据步语句http://www.biostatistic.net/thread-2045-1-1.html  ---转载---原文作者:biostar(出处: 生物统计家园) 数 ...