P5095 [USACO12OPEN]Bookshelf S
很显然此题要用动态规划,我们设
接下来考虑转移,转移我们可以分两种情况讨论,第一,对于当前这本书,我们要新开一个书架,那么
#include <bits/stdc++.h>
using namespace std;
const int N = 2002;
int n, m, p, k, ans, sum, tot, cnt, a[N], b[N], f[N], c[N], l;
int main()
{
std::cin>> n >> l;
for(int i = 1; i <= n; i++)
{
std::cin >> a[i] >> b[i];
c[i] = c[i - 1] + b[i];
f[i] = 1e9;
}
for(int i = 1; i <= n; i++)
{
f[i] = f[i - 1] + a[i];
tot = a[i];
for(int j = i - 1; j >= 1; j--)
{
if(c[i] - c[j - 1] > l)break;
tot = max(tot, a[j]);
f[i] = min(f[i], f[j - 1] + tot);
}
}
cout << f[n];
return 0;
}