寒冰王座

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13177    Accepted Submission(s): 6718

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

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

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

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

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

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

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

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

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

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

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

 
Output
对于每组测试数据,请你输出死亡骑士最少要浪费多少钱给地精商人作为小费.
 
Sample Input
2
900
250
 
Sample Output
0
50
 
Author
Ignatius.L
 
Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1171 1114 1284 2191 2159
 
本来是学习完全背包的,看到了这一题,这题可以说是很裸的完全背包。但是它有一点特殊就是它的每个物品的花费(重量)和它的价值是一样的,所以状态转移方程:f[j]=max(f[j],f[j-w[i]]+w[i]); 也正是因为这个特殊性,使它不仅仅可以用背包,还可以很多其他方法!
首先是完全背包
#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345
#define M 12 int n;
int w[]={,,,};
int f[N];
int main()
{
int T;cin>>T;
while(T--)
{
scanf("%d",&n);
for(int i=;i<=;i++)
for(int j=w[i];j<=n;j++)
{
f[j]=max(f[j],f[j-w[i]]+w[i]);
}
cout<<n-f[n]<<endl;
}
return ;
}

一种非常直接暴力的做法

#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345
#define M 12 int n;
int main()
{
int T;cin>>T;
while(T--)
{
int ma=;
scanf("%d",&n);
for(int i=;i<=n/;i++)
for(int j=;j<=n/;j++)
for(int k=;k<=n/;k++)
{
int sum=*i+*j+*k;
if(sum<=n)
ma=max(ma,sum);
}
cout<<n-ma<<endl;
}
return ;
}

仔细看题可以发现,无敌药水的价钱正好等于血瓶+魔法药品的价钱,所以无敌药品可以直接忽略了,因此可以把上面的代码稍微优化一下,3重循环变成2重了:

for(int i=;i<=n/;i++)
for(int j=;j<=n/;j++)
{
int sum=*i+*j;
if(sum<=n)
ma=max(ma,sum);
}

仔细看一下两种药品价钱150,200,对于小于150的数直接就是那个数,大于300的都不会超过50,所以大于三百直接%50。在这之间的如果判断%200和%150哪个小,哪个小取哪个。这个复杂度就非常低了,是常数级的。

#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345
#define M 12 int n;
int main()
{
int T;cin>>T;
while(T--)
{
scanf("%d",&n);
if(n>=)
{
if(n>=)
n=n%;
else
if(n%<n%)
n=n%;
else
n=n%;
}
cout<<n<<endl;
}
return ;
}

其实这题用搜索也可以做,bfs

#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345
#define M 12 int n;
int vis[N];
int bfs()
{
memset(vis,,sizeof(vis));
int temp=n;
queue<int>q;
q.push(temp);
while(!q.empty())
{
temp=q.front();
q.pop();
if(!vis[temp-] && temp->=)
{
vis[temp-]=;
q.push(temp-);
}
if(!vis[temp-] && temp->=)
{
vis[temp-]=;
q.push(temp-);
}
}
return temp;
}
int main()
{
int T;cin>>T;
while(T--)
{
int ma=;
scanf("%d",&n);
cout<<bfs()<<endl;
}
return ;
}

HDU 1248 寒冰王座 (水题的N种做法!)(含完全背包)的更多相关文章

  1. HDU 1248 寒冰王座(全然背包:入门题)

    HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...

  2. HDU 1248 寒冰王座 (完全背包)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1248 寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    M ...

  3. HDU 1248寒冰王座-全然背包或记忆化搜索

    寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  4. HDU 1248 寒冰王座(完全背包裸题)

    寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  5. hdu 1248 寒冰王座(暴力)

    寒冰王座 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  6. HDU 1248 寒冰王座(完全背包问题另类解法)

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

  7. HDU 1248 寒冰王座 完全背包

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1248 中文题,大意就不说了. 第一道完全背包题,跟着背包九讲做的. 和0-1背包的区别在于所不同的是每种 ...

  8. HDU 1248 寒冰王座(完全背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 商店里只有三种物品,价格分别为150,200,350.输入钱并计算浪费的钱的最小值,商店不找零. ...

  9. HDU 1248 寒冰王座

    完全背包 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

随机推荐

  1. Python第三方库之openpyxl(11)

    Python第三方库之openpyxl(11) Stock Charts(股票图) 在工作表上按特定顺序排列的列或行中的数据可以在股票图表中绘制.正如其名称所暗示的,股票图表通常被用来说明股价的波动. ...

  2. 九度oj 题目1262:Sequence Construction puzzles(I)_构造全递增序列

    题目描述: 给定一个整数序列,请问如何去掉最少的元素使得原序列变成一个全递增的序列. 输入: 输入的第一行包括一个整数N(1<=N<=10000). 接下来的一行是N个满足题目描述条件的整 ...

  3. 九度oj 题目1159:坠落的蚂蚁

    题目描述: 一根长度为1米的木棒上有若干只蚂蚁在爬动.它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右.如果两只蚂蚁碰头,则它们立即交换速度并继续爬动.三只蚂蚁碰头,则两边的蚂蚁交换速度, ...

  4. Python升级版本2.6到2.7

    CentOS 6 系统默认 Python 版本是:2.6.6 平时在使用中遇到很多的库要求是 2.7.x 版本的库,比如使用 ConfigParser 库,在 2.6 版本库就不支持没有 value ...

  5. hdu 1390

    #include<stdio.h> int main() { int t,n,a[1001]; scanf("%d",&t); while(t--) { sca ...

  6. 点击不同按钮,加载不同的页面(不使用iframe的情况下)

    <button id="button1">Load Html1</button> <button id="button2"> ...

  7. scrapy框架之comand line tool

    一 Global Command 1 startproject https://docs.scrapy.org/en/latest/topics/commands.html#startproject ...

  8. springboot开发 第一个案例之hello,world!

    开发环境:(1)Eclipse Java EE  Version: Neon Release (4.6.0)  Build id: 20160613-1800    (2)apache-maven-3 ...

  9. Find that single one.(linear runtime complexity0

    public class Solution { public int singleNumber(int[] nums) { int temp = 0; for (int i=0;i<nums.l ...

  10. android图片上传

    package com.example.center; import java.io.ByteArrayOutputStream;import java.io.InputStream; import ...