Sicily 1193. Up the Stairs

1193. Up the Stairs

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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

2
3 10 5
0 0
0 0
0 0
2 5 1
2 1
3 0

Sample Output

30

8

// Problem#: 1193
// Submission#: 3175294
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
#include <cmath>
using namespace std;

int main() {

    std::ios::sync_with_stdio(false);
    
    int caseNum;
    cin >> caseNum;
    while (caseNum--) {
        short man[1001];
        int n, s, b;
        cin >> n >> s >> b;
        int Max = -1;
        bool gogogo = false;
        if (b % n == 0) gogogo = true;
        for (int i = 0; i < n; i++) {
            int f, box;
            cin >> f >> box;
            if (gogogo) {
                if (box && s + s - f > Max) Max = s + s - f;
                if (!box && f > Max) Max = f;
            } else {
                if (box) man[i] = s - f + s;
                else man[i] = f;
            }
        }
        if (!gogogo) sort(man, man + n);
        if (b % n == 0) cout << (b / n - 1) * 2 * s + Max + s << endl;
        else cout << b / n * 2 * s + man[b % n - 1] + s << endl;
    }


    //getchar();
    //getchar();
    
    return 0;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值