寒冰王座

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. centos 装 android studio

    一.安装前的准备 1.1.如果你还没有安装ubuntu 14.04 LTS系统,请参考下面的文章安装 http://blog.csdn.net/lsyz0021/article/details/521 ...

  2. C++中的临时变量

    临时变量有两个特征: 1.invisiable,在程序代码中没有显式出现 2 没有名字 non - named. 出现临时变量/对象的场合1.函数的返回值2. 参数传递 值传递 by - value ...

  3. Dell Omsa在Linux服务器上安装部署

    前言 本页详述了在一台Linux(RHEL6.4 x86_64)服务器上部署安装OMSA的通用做法,包括OMSA软件的获取方法和安装步骤. 演示环境: PowerEdge R620, RHEL 6.4 ...

  4. xtu数据结构 I. A Simple Tree Problem

    I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld     ...

  5. Python 频繁读取Mysql相关问题

    1.需要频繁select大量数据,时间长.消耗内存大,如何解决mysql性能问题? 如果对返回的结果数量没有要求,可以控制返回的数量: cursor.fetchmany(size=1000) 这样是只 ...

  6. HDU——2444The Accomodation of Students(BFS判二分图+最大匹配裸题)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  7. You need to install the perl-doc package to use this program

    You need to install the perl-doc package to use this program 解决方案:apt-get install perl-doc

  8. 济南学习 Day 5 T1 晚

    取模(mod) [题目描述] 有一个整数a和n个整数b_1, …, b_n.在这些数中选出若干个数并重新排列,得到c_1,…, c_r.我们想保证a mod c_1 mod c_2 mod … mod ...

  9. MarsEdit 快速插入代码

    <div class="cnblogs_Highlighter"> <pre class="brush:objc;gutter:true;"& ...

  10. hdu 4587 2013南京邀请赛B题/ / 求割点后连通分量数变形。

    题意:求一个无向图的,去掉两个不同的点后最多有几个连通分量. 思路:枚举每个点,假设去掉该点,然后对图求割点后连通分量数,更新最大的即可.算法相对简单,但是注意几个细节: 1:原图可能不连通. 2:有 ...