HDU 1248 寒冰王座 (水题的N种做法!)(含完全背包)
寒冰王座
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13177 Accepted Submission(s): 6718
死亡骑士:"我要买道具!"
地精商人:"我们这里有三种道具,血瓶150块一个,魔法药200块一个,无敌药水350块一个."
死亡骑士:"好的,给我一个血瓶."
说完他掏出那张N元的大钞递给地精商人.
地精商人:"我忘了提醒你了,我们这里没有找客人钱的习惯的,多的钱我们都当小费收了的,嘿嘿."
死亡骑士:"......"
死亡骑士想,与其把钱当小费送个他还不如自己多买一点道具,反正以后都要买的,早点买了放在家里也好,但是要尽量少让他赚小费.
现在死亡骑士希望你能帮他计算一下,最少他要给地精商人多少小费.
注意:地精商店只有题中描述的三种道具.
900
250
50
#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种做法!)(含完全背包)的更多相关文章
- HDU 1248 寒冰王座(全然背包:入门题)
HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...
- HDU 1248 寒冰王座 (完全背包)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1248 寒冰王座 Time Limit: 2000/1000 MS (Java/Others) M ...
- HDU 1248寒冰王座-全然背包或记忆化搜索
寒冰王座 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 1248 寒冰王座(完全背包裸题)
寒冰王座 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- hdu 1248 寒冰王座(暴力)
寒冰王座 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU 1248 寒冰王座(完全背包问题另类解法)
寒冰王座 Problem Description 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店 ...
- HDU 1248 寒冰王座 完全背包
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1248 中文题,大意就不说了. 第一道完全背包题,跟着背包九讲做的. 和0-1背包的区别在于所不同的是每种 ...
- HDU 1248 寒冰王座(完全背包)
http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 商店里只有三种物品,价格分别为150,200,350.输入钱并计算浪费的钱的最小值,商店不找零. ...
- HDU 1248 寒冰王座
完全背包 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
随机推荐
- 强制停止及删除(卸载)Windows服务
1. 安装服务: CMD 打开命令行窗口:C:\> 运行:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe& ...
- arrive 和reach 的区别
例如:He arrived yesterday. 没宾语的话就用arrive了reach作抵达讲时是及物动词,后面要宾语的 分清arrive和reach的区别arrive是不及物动词,后面不能直接加地 ...
- xshell连接linux
一些命令和快捷键: Ctrl + Alt 切换linux和windows的鼠标 Ctrl + c 或 Ctrl + d退出>状态 在xshell终端输入exit,退出与linux服务器的连接 登 ...
- 电子邮件中的to、cc、bcc
电子邮件中的to.cc(carbon copy)和bcc(blind carbon copy),分别是收件人.抄送.密送 to 收件人 你想要给其发邮件的人 cc 抄送人 cc和to是一样的,但是cc ...
- luogu3834 【模板】可持久化线段树 1(主席树)
关于空间,第零棵树是 \(4n\),其后每棵树都要多来 \(\log(n)\) 的空间,所以我是开 \(n(4+\log(n))\) 的空间. 关于借用节点: 图片来自这里 #include < ...
- 图论trainning-part-1 H. Qin Shi Huang's National Road System
H. Qin Shi Huang's National Road System Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO f ...
- Spring Cloud 从入门到精通
Spring Cloud 是一套完整的微服务解决方案,基于 Spring Boot 框架,准确的说,它不是一个框架,而是一个大的容器,它将市面上较好的微服务框架集成进来,从而简化了开发者的代码量. 本 ...
- BZOJ 4719 [Noip2016]天天爱跑步 ——树链剖分
一直以为自己当时是TLE了,但是再看发现居然WA? 然后把数组扩大一倍,就A掉了.QaQ 没什么好说的.一段路径分成两段考虑,上升的一段深度+时间是定值,下降的一段深度-时间是定值,然后打标记统计即可 ...
- UOJ 274 【清华集训2016】温暖会指引我们前行 ——Link-Cut Tree
魔法森林高清重置, 只需要维护关于t的最大生成树,然后链上边权求和即可. 直接上LCT 调了将近2h 吃枣药丸 #include <cstdio> #include <cstring ...
- mysql解压之后的安装
远程连接报错(error:10061)看这篇:https://www.cnblogs.com/zipon/p/5877820.html 从5.6.20之后root会自动生成一个随机密码在/root/. ...