2013-07-22 18:21:42Morris

[UVA][二分答案] 12190 - Electric Bill

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 $ sim$ 100 2
101 $ sim$ 10000 3
10001 $ sim$ 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 x 100 + 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 (1$ le$A, B$ 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

題目描述:

電費帳單是根據消耗的能量計算,然而如果你跟鄰居的能量加起來,則會得到帳單 A 元,而如果分開算的話
兩個帳單則會差 B 元。而你使用的能量比鄰居還少,問事實上妳單獨付會需要多少錢。

題目解法:

考慮對 A元 二分搜尋,藉此得到兩個人消耗的總能量,再對總能量二分搜,對 B 比較差值。

#include <stdio.h>
long long bill(long long energy) {
long long consume = 0;
if(energy <= 100)
return consume + energy*2;
consume += 100*2;
if(energy <= 10000)
return consume + (energy-100)*3;
consume += (10000-100)*3;
if(energy <= 1000000)
return consume + (energy-10000)*5;
consume += (1000000-10000)*5;
return consume + (energy-1000000)*7;
}
int main() {
long long A, B;
while(scanf("%lld %lld", &A, &B) == 2) {
if(A+B == 0) break;
long long l = 0, r = A, m;
long long total, tmp, X, Y;
while(l <= r) {
m = (l+r)/2;
tmp = bill(m);
if(tmp == A) {
total = m;
break;
}
if(tmp > A)
r = m-1;
else
l = m+1;
}
l = 0, r = total;
while(l <= r) {
m = (l+r)/2;
X = bill(m), Y = bill(total-m);
//X:you, Y:neighbor
if(Y-X == B) {
printf("%lld\n", X);
break;
}
if(X > Y || Y-X < B)
r = m-1;
else
l = m+1;
}
}
return 0;
}
/*
X+Y = A
X-Y = B
*/