AcWing 4552. 免费馅饼
\(f_{t,x}\) 表示,现在时间为 \(t\),位于的位置为 \(x\) 最多可以接到的馅饼个数,可得状态转移方程:
\[\begin{aligned}f_{t,x}=\left\{\begin{array}{rcl} \max\{f_{t-1,x},f_{t-1,x-1},f_{t-1,x+1}\},0<x<10\\\max\{f_{t-1,x},f_{t-1,x+1}\},x=0\\\max\{f_{t-1,x},f_{t-1,x-1}\},x=10\end{array}\right.\end{aligned}
\]
// #define FILE_INPUT
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#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--)
void Init();
void Solve();
signed main() {
cin.sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#ifdef FILE_INPUT
freopen("input.in", "r", stdin);
#endif
int T = 1;
// cin >> T;
while (T--) {
Init();
Solve();
}
return 0;
}
using LL = long long;
using ULL = unsigned long long;
const int Mod = 1e9 + 7;
const int Inf = 0x3f3f3f3f;
const LL InfLL = 0x3f3f3f3f3f3f3f3f;
const int N = 1e5 + 10;
int n, f[N][20], c[N][20];
void Init() {
}
void Solve() {
while (cin >> n, n) {
memset(f, -0x3f, sizeof(f));
memset(c, 0, sizeof(c));
while (n--) {
int x, t; cin >> x >> t;
c[t][x]++;
}
int ans = 0;
f[0][5] = 0;
rep(t, 1, 1e5) {
rep(x, 0, 10) {
if (x == 0) f[t][x] = max(f[t - 1][x], f[t - 1][x + 1]) + c[t][x];
else if (x == 10) f[t][x] = max(f[t - 1][x], f[t - 1][x - 1]) + c[t][x];
else f[t][x] = max({f[t - 1][x - 1], f[t - 1][x + 1], f[t - 1][x]}) + c[t][x];
}
}
rep(x, 0, 10) ans = max(ans, f[100000][x]);
cout << ans << "\n";
}
}

浙公网安备 33010602011771号