题意
n件物品,第i件物品收银时需耗费ti秒,价格为ci;
在收银过程中每秒可以偷一件没有结账的物品;
问:最少需要多少钱可以带走这些物品?
题解
[背包模型]
第一次尝试:
状态表示:
dp[i][j]: 所有 到第i个可操作时间为j 的方案
属性:
Min
集合划分:
1. 当前付费
2. 当前不付费
发现集合划分比较困难
正解:
把题意换个理解方式:每买一个(花费w[i])就会另外送t[i]个,
即是 花费w[i], 买 t[i]+1 个东西
问:买完 n 个最少需药花费。
那就容易想到 01 背包了
状态表示:
dp[i][j]: 前 i 个,买 j 个 的方案集合
属性:
Min
集合划分:
1. j <= t[i] + 1 (表示 单买 i 这一件)
dp[i][j] = min(dp[i-1][j], c[i]);
2. j > t[i] + 1
dp[i][j] = min(dp[i-1][j], dp[i-1][j-(t[i]+1)] + c[i])
收获:
题意转换, 找等价条件
Code
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
#define read(a) scanf("%d", &a)
#define readl(a) scanf("%lld", &a)
#define readf(a) scanf("%lf", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define Buff ios::sync_with_stdio(false)
#define mem(a) memset(a, 0, sizeof a)
#define pb push_back
const int INF = 1e9 + 7;
const int N = 2e3 + 7;
const int M = 1e6 + 7;
const ll mod = 1e9 + 7;
ll dp[N][N], c[N];
int t[N];
signed main()
{
Buff;
int n;
cin >> n;
for(int i = 1; i <= n; i ++) cin >> t[i] >> c[i];
memset(dp, 0x7f, sizeof dp);
dp[0][0] = 0;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j++)
{
if(j <= (t[i] + 1))
dp[i][j] = min(dp[i-1][j], c[i]);
else
dp[i][j] = min(dp[i-1][j], dp[i-1][j-(t[i]+1)] + c[i]);
}
cout << dp[n][n] << "\n";
return 0;
}