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):


Range Price
(Crazy-Watt-hour) (Americus)
1  100 2
101  10000 3
10001  1000000 5
> 1000000 7
   


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 2 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 2 x100 + 3 x 300 = 1100. You have to pay 2 x 100 + 3 x 50 = 350 Americus, while your neighbor must pay 2 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 (1A, B109). 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
*/


 

你可能感兴趣的:(Electric Bill(二分))