hdu1248

来源:互联网 发布:淘宝衣服款式怎么设置 编辑:程序博客网 时间:2024/05/19 03:21

寒冰王座

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10842    Accepted Submission(s): 5504
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1248

Problem Description
不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前.

死亡骑士:"我要买道具!"

地精商人:"我们这里有三种道具,血瓶150块一个,魔法药200块一个,无敌药水350块一个."

死亡骑士:"好的,给我一个血瓶."

说完他掏出那张N元的大钞递给地精商人.

地精商人:"我忘了提醒你了,我们这里没有找客人钱的习惯的,多的钱我们都当小费收了的,嘿嘿."

死亡骑士:"......"

死亡骑士想,与其把钱当小费送个他还不如自己多买一点道具,反正以后都要买的,早点买了放在家里也好,但是要尽量少让他赚小费.

现在死亡骑士希望你能帮他计算一下,最少他要给地精商人多少小费.
 

Input
输入数据的第一行是一个整数T(1<=T<=100),代表测试数据的数量.然后是T行测试数据,每个测试数据只包含一个正整数N(1<=N<=10000),N代表死亡骑士手中钞票的面值.

注意:地精商店只有题中描述的三种道具.
 

Output
对于每组测试数据,请你输出死亡骑士最少要浪费多少钱给地精商人作为小费.
 

Sample Input
2900250
 

Sample Output
050
解题思路:
这是一道完全背包的题。
所谓完全背包,通常是有N种物品和一个容量为V的背包,每种物品都有无限件可用。第i种物品的体积是c,
价值是w。求解将哪些物品装入背包可使这些物品的体积总和不超过背包容量,且价值总和最大。
这里的面值150、200、350就是相应的w,本题可以将物品价值看成物品体积,此时体积最大的时候也就是
价值最大的时候,因为体积最大不会超过n,所以dp[n]<=n。
动态转移方程:
附:关于完全背包的详解:http://images.cnitblog.com/i/607724/201403/291641427195013.gif
完整代码:
#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;int v[3] = {150 , 200 , 350};int dp[1000001];int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    int T;    scanf("%d",&T);    while(T--)    {        int n;        scanf("%d",&n);        memset(dp , 0 , sizeof(dp));        for(int i = 0 ; i < 3 ; i ++)        {            for(int vi = v[i] ; vi <= n ; vi ++)            {                dp[vi] = max(dp[vi] , dp[vi - v[i]] + v[i]);            }        }        printf("%d\n",n-dp[n]);    }}


0 0