Battery Charging

  • Problem Description

Recently, qbwj bought a powerful cellphone which performs even better than most computers. He spent lots of time on it. However, this cellphone has a very strange feature which confused qbwj. There is a battery with infinite capacity in the cellphone. On each day, qbwj has three choices: use the cellphone, charge the battery or do nothing. He cannot do using and charging on the same day. Charging on the kthkth day adds kk units of power to the battery. If qbwj chooses to use the cellphone on the kthkth day, it would consume kk units of power. Note that qbwj can choose to use the cellphone if and only if there are enough units of power in the battery.

Despite of the strengths of the cellphone, qbwj couldn’t suffer it any more. So he decides to sell it at the end of TthTth day. Today is the Sth day and the battery is empty now. He wants to know how many days at maximum he can use the cellphone before selling it out.

Note that qbwj can still choose to use the cellphone on both the SthSth day and TthTth day.

  • Input
    The first line of the input will be an integer NN (N≤10000N≤10000) indicating the number of cases.

For each test case, two integers are given on a single line: SS TT. 1≤S≤T≤1081≤S≤T≤108.

  • Output
    Print Case #k: d in a single line for each test case, in which kk represents the case number which starts from 11, and dd is the answer.

  • Sample input and output
    Sample Input
    3
    3 6
    3 9
    1 100000
    Sample Output
    Case #1: 1
    Case #2: 3
    Case #3: 49994
    Hint
    For the first sample, we charge the cellphone on the 3rd3rd, 4th4th, 5th5th day and use it on the 6th6th day.

  • 分析

  • 这是一道贪心题
    我们要尽可能多的获得使用手机的天数,最好的方法就是在电量足够的时候尽早的使用手机。
    所以在第s天充电s
    第s+1天充电s+1,总电量2*s+1
    第s+2天用电s+2,总电量s-1
    也就是说每两天要使用s中的一格电,且在s+3天的时候必须继续充电,每两天成一个循环。
    所以过了2*s的时候刚好把s格电用完,再加上原来的s天。所用天为3*s,其中有s天是用电的。剩余部分再除以2相加得用电总天数。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    long long i,s,t,n,tot;
    scanf("%lld",&n);
    for (i=0;i<n;i++)
    {
        scanf("%lld %lld",&s,&t);
        tot=0;
        while (t>=3*s+1)
        {
            tot+=s;
            s=3*s+1;
        }
        tot+=(t-s)/2;
        printf("Case #%lld: %lld\n",i+1,tot);
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值