https://codeforces.com/gym/102082
题意:给你一个车厢和一些人,这些人都坐在座位上,求这些人全部出去的时间最小值。
分析:每个人的路线都是固定的,那么只要前方有空位就走,没空位就等着,那么最终的答案应该是一样的。
然而直接暴力bfs的话,复杂度。
然后我们考虑一下每个人的路线,如果这两个人在没有阻挡的情况下,到达终点的时间是是一样的话,那么必定会在某一点相遇,此时就需要有一个人要等一个时刻,如果有第三个人的话,这第三个就要等两个时刻,那么我们直接算出每个人到终点的时间,排序后依次后延即可。
#include "bits/stdc++.h"
namespace fastIO {
#define BUF_SIZE 100000
bool IOerror = 0;
inline char nc() {
static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
if (p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, BUF_SIZE, stdin);
if (pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
inline void read(int &x) {
char ch;
while (blank(ch = nc()));
if (IOerror) return;
for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
}
#undef BUF_SIZE
};
using namespace fastIO;
using namespace std;
const int mod = 1e9 + 7;
int num[500004];
int main() {
int n, s, p;
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;
}