Electric Bill(二分)

1、https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2477

2、题目:

It's year 2100. Electricity has become very expensive. Recently, your electricity company raised the power rates once more. The table below shows the new rates (consumption is always a positive integer):


RangePrice
(Crazy-Watt-hour)(Americus)
$ \sim$ 1002
101 $ \sim$ 100003
10001 $ \sim$ 10000005
> 10000007
  


This means that, when calculating the amount to pay, the first 100 CWh have a price of 2 Americus each; the next 9900 CWh (between 101 and 10000) have a price of 3 Americus each and so on.

For instance, if you consume 10123 CWh you will have to pay x 100 + 3 x 9900 + 5 x 123 = 30515 Americus.

The evil mathematicians from the company have found a way to gain even more money. Instead of telling you how much energy you have consumed and how much you have to pay, they will show you two numbers related to yourself and to a random neighbor:

A:
the total amount to pay if your consumptions were billed together; and
B:
the absolute value of the difference between the amounts of your bills.

If you can't figure out how much you have to pay, you must pay another 100 Americus for such a ``service". You are very economical, and therefore you are sure you cannot possibly consume more than any of your neighbors. So, being smart, you know you can compute how much you have to pay. For example, suppose the company informed you the following two numbers: A = 1100 and B = 300. Then you and your neighbor's consumptions had to be 150 CWh and 250 CWh respectively. The total consumption is 400 CWh and then A is x100 + 3 x 300 = 1100. You have to pay x 100 + 3 x 50 = 350 Americus, while your neighbor must pay x 100 + 3 x 150 = 650 Americus, so B is | 350 - 650| = 300.

Not willing to pay the additional fee, you decided to write a computer program to find out how much you have to pay.

Input 

The input contains several test cases. Each test case is composed of a single line, containing two integers A and B, separated by a single space, representing the numbers shown to you (1$ \le$AB$ \le$109). You may assume there is always a unique solution, that is, there exists exactly one pair of consumptions that produces those numbers.

The last test case is followed by a line containing two zeros separated by a single space.

Output 

For each test case in the input, your program must print a single line containing one integer, representing the amount you have to pay.

Sample Input 

1100 300 
35515 27615 
0 0

Sample Output 

350 
2900

 

3、代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    int a,b,sum;
    while(scanf("%d%d",&a,&b))
    {
        sum=0;
        if(a==0&&b==0)
            break;
        if(a>=4979900)
            sum=(a-4979900)/7+1000000;
        else if(a>=29900)
        {
            sum=(a-29900)/5+10000;
        }
        else if(a>=200)
            sum=(a-200)/3+100;
        else if(a>=0)
            sum=a/2;
        int l,r;
        if(sum%2==0)
        {
            l=sum/2;
            r=sum;
        }
        else
        {
            l=sum/2+1;
            r=sum;
        }
        int sum1=0,sum2=0;
        int mid;
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(mid>=1000000)
            {
                sum1=4979900+(mid-1000000)*7;
                if(sum-mid>=1000000)
                {
                    sum2=4979900+(sum-mid-1000000)*7;
                }
                else if(sum-mid>=10000)
                {
                    sum2=29900+5*(sum-mid-10000);
                }
                else if(sum-mid>=100)
                {
                    sum2=2*100+(sum-mid-100)*3;
                }
                else if(sum-mid>=0)
                    sum2=2*(sum-mid);
                else
                    sum2=0;
            }
            else if(mid>=10000)
            {
                sum1=29900+5*(mid-10000);
                if(sum-mid>=1000000)
                {
                    sum2=4979900+(sum-mid-1000000)*7;
                }
                else if(sum-mid>=10000)
                {
                    sum2=29900+5*(sum-mid-10000);
                }
                else if(sum-mid>=100)
                {
                    sum2=2*100+(sum-mid-100)*3;
                }
                else if(sum-mid>=0)
                    sum2=2*(sum-mid);
                else
                    sum2=0;
            }
            else if(mid>=100)
            {
                sum1=2*100+(mid-100)*3;
                if(sum-mid>=1000000)
                {
                    sum2=4979900+(sum-mid-1000000)*7;
                }
                else if(sum-mid>=10000)
                {
                    sum2=29900+5*(sum-mid-10000);
                }
                else if(sum-mid>=100)
                {
                    sum2=2*100+(sum-mid-100)*3;
                }
                else if(sum-mid>=0)
                    sum2=2*(sum-mid);
                else
                    sum2=0;
            }
            else if(mid<100)
            {
                sum1=2*mid;
                if(sum-mid>=1000000)
                {
                    sum2=4979900+(sum-mid-1000000)*7;
                }
                else if(sum-mid>=10000)
                {
                    sum2=29900+5*(sum-mid-10000);
                }
                else if(sum-mid>=100)
                {
                    sum2=2*100+(sum-mid-100)*3;
                }
                else if(sum-mid>=0)
                    sum2=2*(sum-mid);
                else
                    sum2=0;
            }
            if(sum1-sum2>b)
                r=mid-1;
            else if(sum1-sum2<b)
                l=mid+1;
            else
                break;
        }
        printf("%d\n",sum2);

    } return 0;
}


/*
85955 55055
*/


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值