CF939F Cutlet
首先考虑暴力 dp,设
明显会 TLE 的,考虑优化。发现有大部分空间全部浪费掉了,我们可以把状态改成
可以发现,一个区间顶多会翻面
当我们在这个区间,只翻
当我们在这个区间,只翻
//#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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!