HDU 5881

Tea is good. 

Tea is life. 

Tea is everything. 

The balance of tea is a journey of pursuing balance of the universe. 

Alice knows that. 

Alice wants to teach you the art of pouring tea. 

Alice has a pot of tea. 

The exact volume of tea is not important. 

The exact volume of tea is at least  LL. 

The exact volume of tea is at most RR. 

Alice put two empty cups between you and her. 

Alice wants the two cups filled by almost equal volume of tea. 

Yours cannot be 11 unit more than hers. 

Hers cannot be 11 unit more than yours. 

Alice wants you to pour the tea. 

Alice wants you to pour until the pot is almost empty. 

Alice wants no more than 11 unit volume of tea remaining in the pot. 

You cannot read the residue volume of tea remaining in the pot. 

You can only know the tea status in the pot, empty or not. 

Alice does not want you to pour the tea too many times. 

You better pour as few times as possible.

InputThere are multiple cases. 
For each case, there is one line of two integers LL and RR, separated by single space. 

Here are some analyses about sample cases. 
For the first case, pouring 11 unit into one cup will satisfy Alice. 
For the second case, it is clearly that you cannot only pour once to reach the desired balance, but she can achieve it by pouring twice. 
First you pour 1.51.5 units into one cup, then you attempt to pour another 1.51.5 units into the other cup. 
Since the lower bound is 22, at least 0.50.5 unit remains in the pot after the first pouring. 
If the initial volume is in range [2,3][2,3], the second cup will have volume in range [0.5,1.5][0.5,1.5] which is balanced with 1.51.5 unit in the first cup, and at most 11 unit remain after these two attempts. 

About 10001000 test cases, and 0LR10160≤L≤R≤1016.
OutputFor each case, there should be a single integer in a single line, the least number of pouring attempts.Sample Input

2 2
2 4

Sample Output

1
2


题意:
  有一个茶壶,茶壶内部水的体积为 [L,R] ,L,R为整数。现在有两个杯子,杯子的体积无限。我可以用茶壶向杯子里倒水,而且可以准确的倒出固定体积的水(如果水的剩余量够的话)。
  但是有两个要求,一个是最后两个杯子内的水不能相差大于 1,第二个条件是 茶壶内可以剩余不超过1体积的水。
问:最小的倒水次数

  如果一开始就往杯子里到 一半体积的水,然后再向另一个杯子到剩余水的话,是不可行的。因为你并不知道茶壶内有多少水,所以你也没法一次到一半体积的水,除非L==R。
  首先我写了一些特判, (r=0),(l=r),(l=0),(r=2)先特判一下这些情况
  
  然后就只剩两类情况了,
  ① r-l<=2
    这种情况下只需要两次就可以完成了,当 l为奇数时 向 第一个杯子里到 l/2+1 然后将剩余的水全部倒入第二个杯子就完了。
   
 当 l 为偶数时 向第一个杯子到 l/2+0.5 然后将剩余的水全部倒入第二个杯子就完了

   ② r-l>2
    先按照①中的方式向第一个杯子倒水,然后我们需要向第二个杯子里倒入 l-(第一个杯子水)+2 体积的水,然后再向一中倒入2体积水,如此反复一直到耗光水。

  最后 ①和②的计算式其实是等价的。均为 2+(r-l-2)/2
    
#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
int main(){
    ll l, r;
    while(scanf("%lld%lld",&l,&r)!=EOF){
        if(r==0){ // 0 0
            printf("0\n");
        }else if(l==r){
            if(l==1)printf("0\n");// 1 1
            else if(l==2)// 2 2
                printf("1\n");
            else
                printf("2\n");// 3 3
        }else if(l==0){
            printf("%lld\n",(r-1)/2+(r>1)); //0 6
        }else if(r==2)
            printf("1\n");
        else if(r-l<=2)
            printf("2\n");

        else printf("%lld\n",2+(r-l-2)/2);
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/kongbb/p/11348452.html
hdu
今日推荐