2017年河南省ACM省賽 Problem H: Intelligent Parking Building
問題 H: Intelligent Parking Building
時間限制: 1 Sec 記憶體限制: 128 MB題目描述
There is a new revolution in the parking lot business: the parking building. The concept is simple: you drive your car into the elevator at the entrance of the building, and the elevator and conveyor belts drag the car to an empty parking spot, where the car remains until you pick it up. When you return, the elevator and conveyor belts move your car back to the entrance and you’re done.
The layout of the building is simple. There is one central elevator that transports the cars between the different floors. On each floor there is one giant circular conveyor belt on which the cars stand. This belt can move in clockwise and counterclockwise direction. When the elevator arrives on a floor, it becomes part of the belt so that cars can move through it.
At the end of the day the building is usually packed with cars and a lot of people come to pick them up. Customers are processed in a first come first serve order: the elevator is moved to the floor of the first car, the conveyor belt moves the car on the elevator, the elevator is moved down again, and so on. We like to know how long it takes before the last customer gets his car. Moving the elevator one floor up- or downwards takes 10 seconds and moving the conveyor belt one position in either direction takes 5 seconds.
輸入
On the first line one positive number: the number of testcases, at most 30. Each test case specifies:
- One line with two integers h and l with 1 ≤ h ≤ 50 and 2 ≤ l ≤ 50: the height of the parking tower and the length of the conveyor belts.
- h lines with l integers: the initial placement of the cars. The jth number on the ith line describes the jth position on the ith floor. This number is −1 if the position is empty, and r if the position is occupied by the rth car to pick up. The positive numbers form a consecutive sequence from 1 to the number of cars. The entrance is on the first floor and the elevator (which is initially empty) is in the first position. There is at least one car in the parking tower.
輸出
For each test case generate a single line containing a single integer that is the number of seconds before the last customer is served.
樣例輸入
3
1 5
1 -1 -1 -1 2
1 5
2 -1 -1 -1 1
3 6
-1 5 6 -1 -1 3
-1 -1 7 -1 2 9
-1 10 4 1 8 -1
樣例輸出
5
10
320
提示
這道題其實很簡單,只不過大部分人是因為沒正確理解題意,題意:一個地下停車場,高h層,每層l個車位,-1表示該位置為空,正整數表示運出車輛的序號,現在需要按照序號運出車輛,運出過程:電梯初始在1層,每上下移動一層需要10秒,每層有一個轉盤,車位均在轉盤上,可以順時針轉,也可以逆時針轉,初始時電梯對應轉盤的第一個位置,每次轉盤轉動之後不再回到初始位置,每轉動一個車位需要5秒。每次將需要運出的車轉到電梯的位置後還需要將電梯返回1層。現在需要計算最小的將車全部運出的時間。程式碼
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int a[55][55];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int h,l,sum=0,maxn=-1,num=1,p[55];
for(int i=0;i<55;i++)
p[i]=1;///存放每層電梯對應的轉盤位置,初始都為轉盤的第一個位置
scanf("%d %d",&h,&l);
for(int i=1;i<=h;i++)
{
for(int j=1;j<=l;j++)
{
scanf("%d",&a[i][j]);
maxn=max(maxn,a[i][j]);///記錄最大的序號,用來後面跳出迴圈
}
}
for(int i=1;i<=h;i++)
{
for(int j=1;j<=l;j++)
{
if(a[i][j]==num)
{
sum+=20*(i-1);///每層需要10秒返回需要同樣的時間,所以直接層數的差值乘以20
sum+=5*min(abs(p[i]-j),l-(abs(p[i]-j)));///判斷順時針轉還是逆時針轉動轉盤次數最少,然後最少次數乘以5
p[i]=j;///把第i層的電梯對應的轉盤位置變為j;
num++;///序號增加
i=1;
j=0;
}
if(num>maxn)break;///如果全部車都運出則跳出迴圈
}
if(num>maxn)break;///如果全部車都運出則跳出迴圈
}
printf("%d\n",sum);
}
}