hdu 4803 Poor Warehouse Keeper(贪心)

2024-03-30 06:08

本文主要是介绍hdu 4803 Poor Warehouse Keeper(贪心),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

hdu 4803 Poor Warehouse Keeper(贪心)

Jenny is a warehouse keeper. He writes down the entry records everyday. The record is shown on a screen, as follow: 

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?
Input
There are several (about 50, 000) test cases, please process till EOF. 
Each test case contains one line with two integers x(1 <= x <= 10) and y(1 <= y <= 10  9) separated by a single space - the expected number shown on the screen in the end.
Output
For each test case, print the minimal number of pressing of the buttons, or “-1”(without quotes) if there’s no way to achieve his goal.
Sample Input
1 1
3 8
9 31
Sample Output
0
5
11 
Hint
For the second test case, one way to achieve is:
(1, 1) -> (1, 2) -> (2, 4) -> (2, 5) -> (3, 7.5) -> (3, 8.5)


题目大意:有以个屏幕可以显示两个值,一个是数量x,一个是总价y。有两种操作,一种是加一次总价,变成x,x+y;一种是加一个数量,这要的话总价也会相应加上一个的价钱,变成x+1,y+y/x。总价显示的为取整后的整数,小数部分忽略。给定一个目标x,y,初始状态为1,1,求最少需要多少次可以目标状态,不可以达到的话输出-1.

解题思路:如果是加一次总价的话,单价就在变大;如果是加一次数量的话,单价是不变的。总而言之,单价是只会往上涨,而不会往下降的。
然后物品的数量也必须从1变成x,也就是说至少要加x-1次单价才可以,那么如果单价过大ss(x1)y+1肯定是不予许的。所以对于每一个i(数量)来说,单价都有一个上限值,以保证说在增加数量的时候不会导致总价溢出。
设当前数量为i,临界总价为t,那么就有
y+1>txi
t<(y+1)ix

    即,每次对于一个数量,尽量加总价,使得单价尽量大,并且保证在和面加数量时不会大于上限,因为单价大的话,加一次数量总价接近目标值的速度会更快。

赛的时候没写出来,后来看了题解才交了上去,用cin会tle。。。

//
//  main.cpp
//  160929
//
//  Created by 刘哲 on 17/4/13.
//  Copyright © 2016年 my_code. All rights reserved.
//
//#include <bits/stdc++.h>#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <list>
#include <bitset>
#include <stack>
#define lowbit(x) (x&-x)
using namespace std;
#define ll long long
const int maxn=1e5+5;
const int INF=0x3f3f3f3f;
int main()
{double x,y;while(~scanf("%lf%lf",&x,&y)){if(x>y){puts("-1");continue;}double k = (y+0.99999)/x;int ans = x-1;double tmp = 1.0;for(int i=1;i<=int(x);i++){double t=i*k;int up=int(t-tmp);tmp += up;tmp=tmp*(i+1)/i;ans+=up;}//cout<<ans<<endl;printf("%d\n",ans);}return 0;
}



这篇关于hdu 4803 Poor Warehouse Keeper(贪心)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/860686

相关文章

usaco 1.3 Barn Repair(贪心)

思路:用上M块木板时有 M-1 个间隙。目标是让总间隙最大。将相邻两个有牛的牛棚之间间隔的牛棚数排序,选取最大的M-1个作为间隙,其余地方用木板盖住。 做法: 1.若,板(M) 的数目大于或等于 牛棚中有牛的数目(C),则 目测 给每个牛牛发一个板就为最小的需求~ 2.否则,先对 牛牛们的门牌号排序,然后 用一个数组 blank[ ] 记录两门牌号之间的距离,然后 用数组 an

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

hdu 1102 uva 10397(最小生成树prim)

hdu 1102: 题意: 给一个邻接矩阵,给一些村庄间已经修的路,问最小生成树。 解析: 把已经修的路的权值改为0,套个prim()。 注意prim 最外层循坏为n-1。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstri