swjtu 1206 小偷阿三
1206: 小偷阿三
Description
阿三去超市买东西,共挑了n件商品,每件商品价值c[i],需要扫描t[i]个单位时间,在扫描中阿三可以花一个单位时间偷他买的其中一件商品,聪明的阿三最少得花多少钱才能得到他挑的全部商品呢?
Input
多组测试样例。每个测试第一行为n(1<=n<=2000)接下来n行,每行包括两个整数t,c。(0 <= t <= 2000, 0 <c <= 1000000)
Output
输出阿三最少花的钱。
Sample Input
4 2 10 0 20 1 5 1 3 3 0 1 0 10 0 100
Sample Output
8 111
一道很巧妙的背包题.
题意可转化为,总容量为n,每个物品的体积为t[i]+1(因为花费t个时间扫描时,可偷t个物品再加这个买的物品),要求使体积≥n+1且价值最小。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cmath> 6 #include <stack> 7 #include <map> 8 #include <queue> 9 #include <algorithm> 10 #define EPS 1e-8 11 using namespace std; 12 typedef long long LL; 13 //////////////////////////////////////////////////// 14 LL F[2010]; 15 int main() 16 { 17 int N; 18 while(scanf("%d",&N)!=EOF) 19 { 20 fill(F+1,F+N+1,1LL<<30); 21 F[0]=0; 22 for(int i=0;i<N;i++) 23 { 24 int c,w; 25 scanf("%d%d",&c,&w); 26 c++; 27 for(int v=N;v>=0;v--) F[v]=min(F[v],F[max(0,v-c)]+w); 28 } 29 printf("%d\n",F[N]); 30 } 31 }