算法學習【17】—— 1193. Up the Stairs

題目來源: http://soj.me/1193

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

思路:開始審題,判斷爲模擬類型。不過在處理模型方面出了點問題,沒有考慮到兩人相交類似穿透,即是可是忽略所謂的交換過程。然後也沒有考慮到週期性,即是可以先定義 每個朋友的初始完成將ground floor的盒子搬上penthouse任務的時間。然後接下來的就是週期性的2*floorNum搬運時間了。最初的搬運過程,是取耗時最長的朋友所花費的時間。然後就沒有然後了。應該簡化模型。

代碼:

/*
Link: http://soj.me/1193
Author: BetaBin
Date: 2012/07/26
*/
#include <stdio.h>
#include <algorithm>

int floorNum;
int friendNum;
int boxleftNum;

int originalUpTime[1005];

int testNum;
int timeCost;
int lastOne;
int i,j,k;

int main()
{
    scanf("%d", &testNum);
    while(testNum--)
    {
        scanf("%d %d %d", &friendNum, &floorNum, &boxleftNum);
        for(i = 0; i < friendNum; i++)
        {
            scanf("%d %d", &j, &k);
            if(k)
            {
                originalUpTime[i] = 3 * floorNum - j;
            }
            else
            {
                originalUpTime[i] = floorNum + j;
            }
        }
        std::sort(originalUpTime, originalUpTime + friendNum);
        lastOne = boxleftNum % friendNum;
        if(lastOne)
        {
            timeCost = (boxleftNum / friendNum) * 2 * floorNum + originalUpTime[lastOne - 1];
        }
        else
        {
            timeCost = (boxleftNum / friendNum - 1) * 2 * floorNum + originalUpTime[friendNum - 1];
        }
        printf("%d\n", timeCost);
    }
    return 0;
}


發佈了121 篇原創文章 · 獲贊 9 · 訪問量 61萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章