题目链接:GYM108082
题意:一群人从位置上通过逃生通道离开,通道不能并行,只能单人有序通行,求全部人出去的最小时间
题解:每个人的路线都是固定的,那么只要前方有空位就走,没空位就等着,然后我们考虑一下每个人的路线,如果这两个人在没有阻挡的情况下,到达终点的时间是是一样的话,那么必定会在某一点相遇,此时就需要有一个人要等一个时刻,如果有第三个人的话,这第三个就要等两个时刻,那么我们直接算出每个人到终点的时间,排序后依次后延即可。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 150005;
typedef pair <int, int> P;
template <class T>
void read(T &x)
{
static char ch;
static bool neg;
for(ch = neg = 0; ch < '0' || '9' < ch; neg |= ch == '-', ch = getchar());
for(x = 0; '0' <= ch && ch <= '9'; (x *= 10) += ch - '0', ch = getchar());
x = neg ? -x : x;
}
const int mod = 1e9 + 7;
int num[500004];
int n, s, p;
int main()
{
read(n),read(s),read(p);
int x, y;
for (int i = 0; i < p; ++i)
{
read(x),read(y);
num[i] = n - x + 1;
if (y <= s)
{
num[i] += s - y + 1;
}
else num[i] += y - s;
}
sort(num, num + p);
int ans = 0;
for (int i = 0; i < p; ++i)
{
ans = max(ans, num[i]);
if (num[i + 1] <= num[i])num[i + 1] = num[i] + 1;
}
cout << ans << endl;
return 0;
}