Emergency Evacuation
Problem Description
The Japanese government plans to increase the number of inbound tourists to forty million in the year 2020, and sixty million in 2030. Not only increasing touristic appeal but also developing tourism infrastructure further is indispensable to accomplish such numbers.
One possible enhancement on transport is providing cars extremely long and/or wide, carrying many passengers at a time. Too large a car, however, may require too long to evacuate all passengers in an emergency. You are requested to help estimating the time required.
The car is assumed to have the following seat arrangement.
• A center aisle goes straight through the car, directly connecting to the emergency exit door at the rear center of the car.
• The rows of the same number of passenger seats are on both sides of the aisle.
A rough estimation requested is based on a simple step-wise model. All passengers are initially on a distinct seat, and they can make one of the following moves in each step.
• Passengers on a seat can move to an adjacent seat toward the aisle. Passengers on a seat adjacent to the aisle can move sideways directly to the aisle.
• Passengers on the aisle can move backward by one row of seats. If the passenger is in front of the emergency exit, that is, by the rear-most seat rows, he/she can get off the car.
The seat or the aisle position to move to must be empty; either no other passenger is there before the step, or the passenger there empties the seat by moving to another position in the same step. When two or more passengers satisfy the condition for the same position, only one of them can move, keeping the others wait in their original positions.
The leftmost figure of Figure C.1 depicts the seat arrangement of a small car given in Sample Input 1. The car have five rows of seats, two seats each on both sides of the aisle, totaling twenty. The initial positions of seven passengers on board are also shown.
The two other figures of Figure C.1 show possible positions of passengers after the first and the second steps. Passenger movements are indicated by fat arrows. Note that, two of the passengers in the front seat had to wait for a vacancy in the first step, and one in the second row had to wait in the next step.
Your task is to write a program that gives the smallest possible number of steps for all the passengers to get off the car, given the seat arrangement and passengers’ initial positions.
Figure C.1. Simple Model
Input
The input consists of a single test case of the following format.
i_1 j_1
…
i_p j_p
Here, r is the number of passenger seat rows, s is the number of seats on each side of the aisle, and p is the number of passengers. They are integers satisfying 1 ≤ r ≤ 500, 1 ≤ s ≤ 500, and 1 ≤ p ≤ 2rs.
The following p lines give initial seat positions of the passengers. The k-th line with ik and jk means that the k-th passenger’s seat is in the ik -th seat row and it is the jk -th seat on that row. Here, rows and seats are counted from front to rear and left to right, both starting from one.
They satisfy 1 ≤ i_k ≤ r and 1 ≤ j_k ≤ 2s. Passengers are on distinct seats, that is, i_k ≠ i_l or j_k ≠ j_l holds if k ≠ l.
Output
The output should be one line containing a single integer, the minimum number of steps required for all the passengers to get off the car.
Sample Input
5 2 7
1 1
1 2
1 3
2 3
2 4
4 4
5 2
Sample Output
9
Sample Input2
500 500 16
1 1
1 2
1 999
1 1000
2 1
2 2
2 999
2 1000
3 1
3 2
3 999
3 1000
499 500
499 501
499 999
499 1000
Sample Output2
1008
Solution:
从座位上出来,和从出口全部进去到座位上的时间是一样的。
座位远的先进去,以减少总体的时间
AC Code:
#include <bits/stdc++.h>
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d", &a, &b)
#define siii(a, b, c) scanf("%d", &a, &c)
#define siiii(a, b, c, d) scanf("%d", &a, &c, &d)
#define pi(a) printf("%d\n", a)
#define pii(a, b) printf("%d %d\n", a, b)
#define piii(a, b, c) printf("%d %d% d\n", a, b, c)
#define piiii(a, b, c, d) printf("%d %d %d %d\n", a, b, c, d)
#define forRange(i, j, k) for(int i = j; i < k; ++i)
#define mem(a, b) memset(a, b, sizeof(a))
#define printArrInRange(a, from, to) for(int i = from; i < to; ++i) printf("%d ", a[i]); putchar('\n')
#define printArr(a) putchar('['); for(auto k : a) printf("%d ", k); putchar(']'); putchar('\n')
#define mCopy(to, from) memcpy(to, from, sizeof(from))
#define testData(f) freopen(f,"r",stdin)
using namespace std;
int const MAXN = 1e6;
int a[MAXN];
int row, col, peopleNum;
int abs(int k) {
return k < 0 ? -k : k;
}
class Point {
public:
int x;
int y;
int dis;
Point() {
}
Point(int x, int y) : x(x), y(y) {
dis = row - x + 1;
if(y <= col){
dis += col - y + 1;
} else dis+= y-col;
}
bool operator<(Point const &p2) const {
return this->dis > p2.dis;
}
};
Point people[MAXN];
int main() {
scanf("%d%d%d", &row, &col, &peopleNum);
for (int i = 0; i < peopleNum; ++i) {
int x, y;
scanf("%d%d", &x, &y);
people[i] = Point(x, y);
}
sort(people, people + peopleNum);
int ans = 0;
for (int i = 0; i < peopleNum; ++i) {
int time = people[i].dis + i;
ans = max(time, ans);
}
printf("%d\n", ans);
return 0;
}