设 (f_{i,j}) 表示前 (i) 道题目 Alice 写了 (j) 道所需耗费的最少时间。
转移的时候枚举第 (i) 道写不写即可。
具体实现参考代码。
#include <bits/stdc++.h>
#define DEBUG fprintf(stderr, "Passing [%s] line %d
", __FUNCTION__, __LINE__)
#define File(x) freopen(x".in","r",stdin); freopen(x".out","w",stdout)
#define int long long
using namespace std;
typedef long long LL;
typedef pair <int, int> PII;
typedef pair <int, PII> PIII;
template <typename T>
inline T gi()
{
T f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const int INF = 0x3f3f3f3f, N = 2013, M = N << 1;
int n, a[N], b[N];
LL sum[N], f[N][N];
signed main()
{
//File("");
int T = gi <int> ();
while (T--)
{
n = gi <int> ();
for (int i = 1; i <= n; i+=1) a[i] = gi <int> ();
for (int i = 1; i <= n; i+=1) b[i] = gi <int> (), sum[i] = sum[i - 1] + b[i];
memset(f, 0x3f, sizeof f);
LL inf = f[1][1];
f[0][0] = 0;
for (int i = 1; i <= n; i+=1)
for (int j = 0; j < i; j+=1)
{
if (f[i - 1][j] >= inf) continue;
f[i][j] = min(f[i][j], f[i - 1][j]); //第 i 道题目不写
if (f[i - 1][j] + a[i] <= sum[i]) //能写第 i 道题
f[i][j + 1] = min(f[i][j + 1], f[i - 1][j] + a[i]); //写第 i 道题目,转移
}
int now = 0;
while (f[n][now + 1] < inf) ++now;
printf("%lld
", now);
}
return 0;
}