Technocup 2021 - Elimination Round 2 F - Cakes for Clones
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<map>
//#include<regex>
#include<cstdio>
#include <iomanip>
#pragma GCC optimize(2)
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
typedef unsigned long long ull;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
ll read()
{
char ch = getchar(); ll x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 5e3 + 10;
int n;
ll t[N],x[N] ,dp[N][N], f[N];
ll dist(int i, int j) {
return abs(x[i] - x[j]);
}
int main() {
n = read();
upd(i, 1, n) {
t[i] = read(), x[i] = read();
}
memset(dp, 0, sizeof(dp));
memset(f, INF, sizeof(f));
t[0] = 0;
f[0] = 0;
dp[0][0] = 1;
upd(i, 0, n) {
if (f[i] <= t[i]) {
f[i + 1] = min(f[i + 1], max(f[i] + dist(i,i + 1), t[i]));
upd(j, i + 1, n+1) {
dp[i][j] |= (f[i] + 2 * dist(i, j) <= t[i]);
dp[i + 1][j] |= (max(t[i], dist(i, j) + f[i]) + dist(j, i + 1) <= t[i + 1]);
}
}
if (t[i] + dist(i,i + 1) <= t[i + 1]) {
upd(j, i + 2, n+1)dp[i + 1][j] |= dp[i][j];
}
if (i + 1 <= n&&dp[i][i+1]) {
upd(j, i + 3, n+1) {
dp[i + 2][j] |= (max(t[i] + dist(i, j), t[i + 1]) + dist(i + 2, j)) <= t[i + 2];
}
f[i + 2] = min(f[i + 2], max(t[i + 1], dist(i, i + 2) + t[i]));
}
}
if (f[n] <= t[n] || dp[n - 1][n]) {
puts("YES");
}
else puts("NO");
return 0;
}
橙橙橙