[UVA] 10576 - Y2K Accounting Bug
Problem B: Y2K Accounting Bug
Problem B: Y2K Accounting Bug |
Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc.
All what they remember is that MS Inc. posted a surplus or a deficit each month of 1999 and each month when MS Inc. posted surplus, the amount of surplus was s and each month when MS Inc. posted deficit, the deficit was d. They do not remember which or how many months posted surplus or deficit. MS Inc., unlike other companies, posts their earnings for each consecutive 5 months during a year. ACM knows that each of these 8 postings reported a deficit but they do not know how much. The chief accountant is almost sure that MS Inc. was about to post surplus for the entire year of 1999. Almost but not quite.
Write a program, which decides whether MS Inc. suffered a deficit during 1999, or if a surplus for 1999 was possible, what is the maximum amount of surplus that they can post.
Input and Output
Input is a sequence of lines, each containing two positive integers s and d. For each line of input, output one line containing either a single integer giving the amount of surplus for the entire year, or output `Deficit' if it is impossible.
Sample input
59 237 375 743 200000 849694 2500000 8000000
Sample Output
116 28 300612 Deficit
題意 :
ACM 這家公司失去了每個月是否盈利還是赤字,
每個月不是賺 $s 就是虧 $d,他們會公布連續五個月的盈虧,
因此每年會公布八次,現在知道這八次都是赤字。
問最多可以獲利多少?
解法:
窮舉月份是否為賺或虧,因此需要 2^12 次,然後檢查符合的條件即可。
#include <stdio.h>
int main() {
int s, d;
while(scanf("%d %d", &s, &d) == 2) {
int y_surplus = -1, year, i, j;
for(year = (1<<12)-1; year >= 0; year--) {
int tot = 0, flag = 1;
for(i = 0; i < 12; i++) {
if((year>>i)&1)
tot += s;
else
tot -= d;
if(i < 8) {
int tmp = 0;
for(j = i; j < i+5; j++)
if((year>>j)&1)
tmp += s;
else
tmp -= d;
if(tmp >= 0) flag = 0;
}
}
if(flag && y_surplus < tot)
y_surplus = tot;
}
if(y_surplus < 0) puts("Deficit");
else
printf("%d\n", y_surplus);
}
return 0;
}