CF939F Cutlet
首先考虑暴力 dp,设 \(f_{i,j}\) 表示现在的时间是 \(i\),然后另一面(现在不在煎的面)已经煎了 \(j\) 分钟,那么先考虑不翻 \(f_{i,j}=\min f_{i-1,j}\),再考虑翻 \(f_{i,j}=\min f_{i-1,i-j}+1\) (默认都取最小)
明显会 TLE 的,考虑优化。发现有大部分空间全部浪费掉了,我们可以把状态改成 \(f_{i,j}\) 表示现在的时间是 \(r_i\) 然后没有煎的面煎了 \(j\) 分钟,接着考虑转移。
可以发现,一个区间顶多会翻面 \(2\) 次,其他的都不优了。
当我们在这个区间,只翻 \(1\) 次时,\(f_{i,j}=\min f_{i-1,R_i-j-k}+1\) (\(k\) 表示原先的面在这个区间内烤了多长时间)。
当我们在这个区间,只翻 \(2\) 次时,\(f_{i,j}=\min f_{i-1,j-k}+2\) (\(k\) 表示反面在这个区间烤了多长时间)。
\(0\le k\le R_i-L_i\) 后面用单调队列优化,滚动数组就行了。
//#define FILE_INPUT
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
bool Mbe;
#define rep(i, a, b) for (int i = a, END##i = b; i <= END##i; i++)
#define per(i, a, b) for (int i = a, END##i = b; i >= END##i; i--)
#define DEBUG(x) cerr << #x << " = " << x << '\n'
using LL = long long;
using ULL = unsigned long long;
inline LL read() {
LL s = 0, fu = 1; char ch = getchar();
while (ch < '0' || ch > '9') ch == '-' ? fu = -1 : 0, ch = getchar();
while (ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
return s * fu;
}
const int Mod = 1e9 + 7;
const int Inf = 0x3f3f3f3f;
const LL InfLL = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 10, M = 110;
struct Node {
int l, r;
bool operator< (const Node& b) const {
return l < b.l;
}
}a[M];
int f[2][N], n, m, q[N], hh = 0, tt = -1;
void Init() {
}
void Solve() {
n = read(), m = read();
memset(f[0], 0x3f, sizeof(f[0]));
rep(i, 1, m) a[i].l = read(), a[i].r = read();
f[0][0] = 0;
sort(a + 1, a + m + 1);
rep(i, 1, m) {
int cur = i & 1, lst = cur ^ 1;
rep(j, 0, n) f[cur][j] = f[lst][j];
hh = 0, tt = -1;
per(j, a[i].r, 0) {
while (hh <= tt && q[hh] < a[i].l - j) hh++;
while (hh <= tt && f[lst][q[tt]] >= f[lst][a[i].r - j]) tt--;
q[++tt] = a[i].r - j;
f[cur][j] = min(f[lst][q[hh]] + 1, f[cur][j]);
}
hh = 0, tt = -1;
rep(j, 0, a[i].r) {
while (hh <= tt && q[hh] < a[i].l + j - a[i].r) hh++;
while (hh <= tt && f[lst][q[tt]] >= f[lst][j]) tt--;
q[++tt] = j;
f[cur][j] = min(f[lst][q[hh]] + 2, f[cur][j]);
}
}
if (f[m & 1][n] == Inf) puts("Hungry");
else {
puts("Full");
printf("%d\n", f[m & 1][n]);
}
}
bool Med;
signed main() {
#ifdef FILE_INPUT
freopen("input.in", "r", stdin);
#endif
int T = 1;
// T = read();
while (T--) {
Init();
Solve();
}
cerr << "Memory: " << fixed << setprecision(3) << (&Med - &Mbe) / 1048576.0 << " MB\n";
cerr << "Time: " << fixed << setprecision(3) << 1e3 * clock() / CLOCKS_PER_SEC << " ms\n";
return 0;
}

浙公网安备 33010602011771号