HDU 4803 贪心

尽可能的让当前的平均值接近最后的平均值才能最快达到终点的情况

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5
 6 int main()
 7 {
 8   //  freopen("a.in" , "r" , stdin);
 9     int x , y;
10     while(scanf("%d%d" , &x , &y) == 2)
11     {
12         if(y<x){
13             puts("-1");
14             continue;
15         }
16         double ave = (y+0.999999)/x;
17         double cur = 1.0;
18         int cnt = x-1 , ax = 1;
19         while(1)
20         {
21             if(ax == x+1) break;
22             int t = (int)(ave*ax - cur);
23             cur += t , cnt += t;
24             cur += cur/ax;
25             ax++;
26         }
27         printf("%d\n" , cnt);
28     }
29     return 0;
30 }
时间: 2024-10-23 06:02:02

HDU 4803 贪心的相关文章

hdu 4803 贪心/思维题

http://acm.hdu.edu.cn/showproblem.php?pid=4803 话说C++还卡精度么?  G++  AC  C++ WA 我自己的贪心策略错了 -- 就是尽量下键,然后上键,最后下键补全,可是例子都过不了..... 题解參考http://www.cnblogs.com/xuesu/p/3967704.html http://www.cnblogs.com/Canon-CSU/p/3451784.html http://blog.csdn.net/keshuai199

HDU 4803 Poor Warehouse Keeper(贪心)

http://acm.hdu.edu.cn/showproblem.php?pid=4803 贪心的策略是,每次尽量加价格,加到能满足条件的最大值,然后加一下数量,这样反复直到到达答案. 然后加到满足条件最大值一步不能模拟,可以推一下公式就能直接算出来了 代码: #include <stdio.h> #include <string.h> const double eps = 1e-9; double x, y; int main() { while (~scanf("%

hdu 4803 Poor Warehouse Keeper(贪心+数学)

题目链接:hdu 4803 Poor Warehouse Keeper 题目大意:有以个屏幕可以显示两个值,一个是数量x,一个是总价y.有两种操作,一种是加一次总价,变成x,x+y:一种是加一个数量,这要的话总价也会相应加上一个的价钱,变成x+1,y+y/x.总价显示的为取整后的整数,小数部分忽略.给定一个目标x,y,初始状态为1,1,求最少需要多少次可以目标状态,不可以达到的话输出-1. 解题思路:如果是加一次总价的话,单价就在变大:如果是加一次数量的话,单价是不变的.总而言之,单价是只会往上

hdu 4105 贪心思想

淋漓尽致的贪心思想 波谷一定是一位数,波峰一位数不够大的时候添加到两位数就一定够大了的. 当在寻找波谷碰到零了就自然当成波谷. 当在寻找波峰时碰到零时,将前面的波谷加到前一个波峰上,让当前的零做波谷,使得波谷的值尽量小,这就是本题最关键的贪心思想,一直想不到. 代码中:a表示前一个值,b表示当前考虑的值,tag为偶数时表示正在寻找波谷,奇数时在寻找波峰. #include<iostream> #include<cstdio> #include<cstring> #inc

HDU 4923 (贪心+证明)

Room and Moor Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that:

hdu 2037 贪心

今年暑假不AC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27361    Accepted Submission(s): 14439 Problem Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" &quo

HDU 4932 贪心

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 191    Accepted Submission(s): 38 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

hdu 4292 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4296 Problem Description Have you ever heard the story of Blue.Mary, the great civil engineer? Unlike Mr. Wolowitz, Dr. Blue.Mary has accomplished many great projects, one of which is the Guanghua Building. T

hdu 4442 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4442 Problem Description WANGPENG is a freshman. He is requested to have a physical examination when entering the university. Now WANGPENG arrives at the hospital. Er-.. There are so many students, and the nu