HDU - 1248 寒冰王座 数学or暴力枚举
思路:
1、暴力枚举每种面值的张数,将可以花光的钱记录下来。每次判断n是否能够用光,能则输出0,不能则向更少金额寻找是否有能够花光的。时间复杂度O(n)
2、350 = 200 + 150,买350的道具可用一个150和200的代替,那么直接考虑200和150的道具即可。首先全部买150的道具,剩下的金额为x,可以看成t = x / 50张50的钱加上r = x % 50的钱。如果t <= n / 150,那么说明这些50的都可以和一个150的买200的道具,否则会剩下一些50的没法用。假设最后剩余k张50,那么小费就是50 * k + t. 时间复杂度O(1)
第一种方法AC代码:
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 10000 + 5;
int a[maxn];
int u[] = {150, 200, 350};
void init() {
memset(a, 0, sizeof(a));
for(int i = 0; i < 70; ++i)
for(int j = 0; j < 55; ++j)
for(int k = 0; k < 30; ++k) {
int sum = i * 150 + j * 200 + k * 350;
if(sum > 10000) continue;
else a[sum] = 1;
}
}
int find(int n) {
if(a[n]) return 0;
//left
int ans = n;
for(int i = n-1; i >= 0; --i) {
if(a[i]) {
ans = n - i;
break;
}
}
return ans;
}
int main() {
init();
int n, T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
printf("%d\n", find(n));
}
return 0;
}
第二种方法AC代码:
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 1e4 + 5;
int main() {
int T, n;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
int u = n % 150, v = n / 150;
if(u == 0) printf("0\n");
else {
int k = u / 50, h = u % 50;
printf("%d\n", h + 50 * (k - min(v, k)));
}
}
return 0;
}
如有不当之处欢迎指出!
HDU - 1248 寒冰王座 数学or暴力枚举的更多相关文章
- 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 寒冰王座 (水题的N种做法!)(含完全背包)
寒冰王座 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU 1248 寒冰王座(完全背包问题另类解法)
寒冰王座 Problem Description 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店 ...
- HDU 1248 寒冰王座(完全背包)
http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 商店里只有三种物品,价格分别为150,200,350.输入钱并计算浪费的钱的最小值,商店不找零. ...
- HDU 1248 寒冰王座 完全背包
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1248 中文题,大意就不说了. 第一道完全背包题,跟着背包九讲做的. 和0-1背包的区别在于所不同的是每种 ...
随机推荐
- python_7_列表
什么是列表? --一种数据类型 -- 形式:[值1,值2,[值a,值b],值3] --可以嵌套 #!/usr/bin/python3 list_a = [1, 2, [3, 'a']] 对于 ...
- linux 中 ping的回传值
今天在学习鸟哥私房菜的过程中,不明白ping的回传值是怎么设置的,后来网上找的结果了,特此记录一下 1 题目大意是指,ping一个网段的机器,如果可以通,就显示UP,如果不通就显示Down,其中一 ...
- maven-assembly-plugin插件的使用方法
一. Assembly 是什么意思? 二. maven-assembly-plugin是什么? 它是maven中针对打包任务而提供的标准插件. 三. maven-assembly-plugin插件的作 ...
- getRequestDispatcher()和response.sendRedirect()
request.getRequestDispatcher()是请求转发,前后页面共享一个request response.sendRedirect()是重新定向,前后页面不是一个request.
- nginx把POST转GET请求解决405问题
405重定向,然后把POST转GET upstream local { server 10.0.1.11:81; } server { listen 81; server_name testf.xxx ...
- #pragma once 与 #ifndef 解析
转自:http://www.cnblogs.com/hokyhu/archive/2009/03/30/1425604.html 为了避免同一个文件被include多次,C/C++中有两种方式,一种是 ...
- 4.ES核心慨念
一. 和lucene的关系 lucene是最先进,功能最强大的搜索库.但是使用复杂(要深入理解其中原理. elasticsearch,基于lucene,隐藏复杂性,提供简单易用的restful api ...
- wpf阻止键盘快捷键alt+space,alt+F4
/// <summary> /// 阻止 alt+f4和alt+space 按键 /// </summary> /// <par ...
- Yii AR中处理多表关联的relations配置
关系型 Active Record官方文档中指出: 两张表之间的关联是根据外键来的,但是这种外键关联虽然在数据容错方面有益处,但是在性能上是个损伤,所以,一般是不定义外键的. 这种情况下,他们之间的关 ...
- python抢小米6自动化脚本
#!/bin/env python # coding=utf-8 from selenium import webdriver import time import unittest class Ge ...