HDU 4803 Poor Warehouse Keeper (贪心+避开精度)
555555,能避开精度还是避开精度吧,,,,我们是弱菜。。
Poor Warehouse Keeper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1672 Accepted Submission(s): 463
There are only two buttons on the screen. Pressing the button in the first line once increases the number on the first line by 1. The cost per unit remains untouched. For the screen above, after the button in the first line is pressed, the screen will be:
The exact total price is 7.5, but on the screen, only the integral part 7 is shown. Pressing the button in the second line once increases the number on the second line by 1. The number in the first line remains untouched. For the screen above, after the button in the second line is pressed, the screen will be:
Remember the exact total price is 8.5, but on the screen, only the integral part 8 is shown. A new record will be like the following:
At that moment, the total price is exact 1.0. Jenny expects a final screen in form of:
Where x and y are previously given. What’s the minimal number of pressing of buttons Jenny needs to achieve his goal?For the second test case, one way to achieve is: (1, 1) -> (1, 2) -> (2, 4) -> (2, 5) -> (3, 7.5) -> (3, 8.5)
一个机器,只有两个按钮,上面的按钮表示数量,下面的表示总价(只显示整数部分)
开始的时候数量和总价都是1,单价为1,然后进行一些操作,让数量变成x,总价变成y,求得是最小操作次数。
如果按数量键,表示增加一个物品,单价不变,数量+1,那么总价=总价/数量*(数量+1),并且显示新的数量
如果按总价键,单价=(总价+1)/数量,那么总价=总价+1,并且显示总价的整数部分。
x很小y很大,暴力宽搜TLE了。
换个角度,求最小操作次数 换成 求最小合法单价的操作次数
因为最终得到了x数量,y的总价,对于一个单价tp,如果tp*x的整数部分==y那么这个单价就是可以的。
而最小操作次数一定是 得到最小合法单价的操作次数。
对于两种操作,按数量键并不会影响单价,按总价键会增加单价,而且增加单价的幅度是随着操作次数增多而下降的,按下数量键会是这个幅度下降的更多。
这样,一共要按下x-1次数量键,每次按数量键前尽量多的按总价键(保证该单价下的x个数量物品不会超过y元),最后会得到一个不超过目标单价(y/x)的最大单价z,该单价下的总价值为z*n如果z*n<y则还需要按下若干次总价键。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<string>
//#include<pair> #define N 1005
#define M 1000005
#define mod 1000000007
#define inf 0x3f3f3f3f
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; ll mul=;
ll x,y;
ll ans;
ll tmi,tma;
ll dmi,dma;
ll dan;
ll num,tot; void ini()
{
ans=;
tmi=y*mul;
tma=(y+)*mul;
dmi=tmi/x;
dma=tma/x;
num=;
dan=mul;
tot=mul;
} void solve()
{
ans=x-;
ll k1,k2;
ll te;
if(dan>=dma){
ans=-;return;
}
for(num=;num<=x;num++){
te=mul/num;
if(dan>=dmi) break;
k1=(dmi-dan+te-)/te;
k2=(dma-dan+te-)/te;
// printf(" num=%I64d ans=%I64d dan=%I64d dmi=%I64d dma=%I64d te=%I64d k1=%I64d k2=%I64d\n",
// num,ans,dan,dmi,dma,te,k1,k2);
if(k1!=k2){
ans+=k1;
dan+=k1*mul/num;
return;
}
else{
dan+=(k1-)*te;
ans+=(k1-);
}
}
} void out()
{
printf("%I64d\n",ans);
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
// for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
while(scanf("%I64d%I64d",&x,&y)!=EOF)
{
//if(n==0 && k==0 ) break;
//printf("Case %d: ",ccnt);
ini();
solve();
out();
} return ;
}
HDU 4803 Poor Warehouse Keeper (贪心+避开精度)的更多相关文章
- hdu 4803 Poor Warehouse Keeper(贪心+数学)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26005267 题目链接:hdu 4803 P ...
- HDU 4803 Poor Warehouse Keeper(贪心)
题目链接 题意 :屏幕可以显示两个值,一个是数量x,一个是总价y.有两种操作,一种是加一次总价,变成x,1+y:一种是加一个数量,这要的话总价也会相应加上一个的价钱,变成x+1,y+y/x.总价显示的 ...
- HDU 4803 Poor Warehouse Keeper
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4803 解题报告:有一个记录器,一共有两个按钮,还有两行屏幕显示,第一行的屏幕显示的是数目,第二行的屏幕 ...
- HDU - 4803 - Poor Warehouse Keeper (思维)
题意: 给出x,y两个值分别代表x个物品,总价为y 有两种变化: 1.使总价+1,数量不变 2.数量+1,总价跟着变化 (y = y + y / x) 思路: 给出目标x,y,计算最少变化次使数量变化 ...
- 2013ACM/ICPC亚洲区南京站现场赛---Poor Warehouse Keeper(贪心)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4803 Problem Description Jenny is a warehouse keeper. ...
- Poor Warehouse Keeper
Poor Warehouse Keeper http://acm.hdu.edu.cn/showproblem.php?pid=4803 Jenny is a warehouse keeper. He ...
- HDU 4952 Poor Mitsui(贪心)
HDU 4957 Poor Mitsui pid=4957" style="">题目链接 思路:利用相邻交换法去贪心就可以.注意容积为0的情况,这是个坑点 代码: ...
- 【贪心】hdu4803 Poor Warehouse Keeper
题意:一开始有1个物品,总价是1.你的一次操作可以要么使得物品数量+1,总价加上当前物品的单价.要么可以使得总价+1,物品数量不变.问你最少要几次操作从初始状态到达有x个物品,总价是y的状态.这里的y ...
- HDU 4802 && HDU 4803 贪心,高精 && HDU 4804 轮廓线dp && HDU 4805 计算几何 && HDU 4811 (13南京区域赛现场赛 题目重演A,B,C,D,J)
A.GPA(HDU4802): 给你一些字符串对应的权重,求加权平均,如果是N,P不计入统计 GPA Time Limit: 2000/1000 MS (Java/Others) Memory ...
随机推荐
- 基本编程题 --python
1.让Python帮你随机选一个饮品吧! import random listC = ['加多宝', '雪碧', '可乐', '勇闯天涯', '椰子汁'] print(random.choices(l ...
- IE6 bug总结
IE6bug总结: 1.双边距bug产生原因 margin的方向与浮动的方向相同 解决方法: 浮动的元素身上加 display:inline; ---------------------------- ...
- python 实例方法,类方法,静态方法,普通函数
python中有实例方法,类方法,静态方法,普通函数 类方法需要@ classmethod 修饰并且有个隐藏参数 cls,实例方法必须有个参数 self, 静态方法必须有 @staticmethod修 ...
- java实现中文或其他语言及标点符号等转换成unicode字符串,或unicode的16进制码转换回文字或符号等
package org.analysisitem20181016.test; public class Code128Test2019052201 { public static final Stri ...
- Mybatis generator自动生成代码包括实体,dao,xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- 验证IP端与数据库Ip端是否重复!!!
select COUNT(id) from house_info_config hic where (hic.ip_start <![CDATA[<=]]> #{ipend} AND ...
- getBean(class )并发下性能较差,有锁.
spring 版本3.1.2 1. spring 并没有缓存 class -> beanDifinition 或者 sington 实例的缓存. 2. 只能先获取所有的beanDifitions ...
- shell脚本,awk如何处理文件中上下关联的两行。
文件d.txt如下内容 ggg 1portals: 192.168.5.41:3260werew 2portals: 192.168.5.43:3260 如何把文件d.txt内容变为如下内容 ggg ...
- 看结果,测试?java中的String类 字符串拆分成字符串数组 判定邮箱地址 字符串比较 参数传递?
看结果1? package com.swift; class ArrayString { public static void main(String[] args) { String str = & ...
- windows 下phpstudy 升级mysql版本5.7
今天在导入sql文件的时候遇到了sql执行错误.最后找到原因是因为mysql版本过低,导致出错 原因:在执行sql的时候出现了两次CURRENT_TIMESTAMP ,最后得知在5.7版本之前都是不支 ...