# Solution 1

CQzhangyu的做法. 非常的妙啊

$\ge t$ 不好做, 走过的位置以后可能又会突然冒出一个人, 决策的顺序位置.

# Code

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#define rep(i, a, b) for (int i = (a), _ = (b); i <= _; ++ i)
#define per(i, a, b) for (int i = (a), _ = (b); i >= _; -- i)
#define For(i, a, b) for (int i = (a), _ = (b); i < _; ++ i)
#define ri rd<int>
using namespace std;
const int N = 1007;
const int INF = 1e9 + 7;

template<class T> inline T rd() {
bool f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 0;
T x = 0; for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; return f ? x : -x;
}

int m, n, cur;
int t[N];
int f[N][N];
int lf, rt;

int main() {
#ifndef ONLINE_JUDGE
freopen("a.in", "r", stdin);
#endif

m = ri(), n = ri(), cur = ri() + 1;

lf = n + 1, rt = 0;
rep (i, 1, m) {
int x = ri() + 1, y = ri();
t[x] = max(t[x], y);
lf = min(lf, x), rt = max(rt, x);
}

memset(f, 0x3f, sizeof f);
f[lf][rt] = max(lf - 1, t[lf]), f[rt][lf] = max(rt - 1, t[rt]);

per (l, rt-lf-1, 0) rep (i, lf, rt - l) {
int j = i + l;
f[i][j] = min(f[i][j], max(t[i], min(f[i-1][j] + 1, f[j+1][i] + ((j + 1) - i))));
f[j][i] = min(f[j][i], max(t[j], min(f[i-1][j] + (j - (i - 1)), f[j+1][i] + 1)));
}

int res = INF;
rep (i, lf, rt) res = min(res, f[i][i] + abs(i - cur));
printf("%d\n", res);

return 0;
}
posted @ 2017-11-01 20:40  _zwl  阅读(...)  评论(...编辑  收藏