ZOJ - Yuyuko and Youmu - 3384
很久没有写博客了。
先上题目:
Saigyouji Yuyuko (西行寺幽々子), the ghost from the calamitous nirvana, is the princess of Hakugyokurou (白玉楼) in the Netherworld, while Konpaku Youmu (魂魄妖夢), half-human half-phantom, is the gardener of Hakugyokurou. Yuyuko is famous as a "hungry ghost" having an immense appetite. So Youmu is alway busy preparing enough food for her.
For the coming n days, knowing the amount of food Yuyuko needs and the amount of food Youmu can prepare at each day, Youmu wants to make an arrangement so that Yuyuko can eat enough food everyday, and the food is as fresh as possible. There are no food remained in Hakugyokurou currently.
Input
There are multiple cases. The first line of each case contains a integer n (1 ≤ n ≤ 1000). The second line contains n integers (1 ≤ ai ≤ 1000000), the amount of food Yuyuko needs at each day. The third line contains n integers (1 ≤ bi ≤ 1000000), the amount of food Youmu can prepare at each day.
Output
If an arrangement exists, output n integers, the amount of food Youmu should prepare at each day. If not, output a line of "Myon".
Sample Input
5 10 10 100 10 10 50 50 50 50 50 9 1 2 4 8 16 32 64 128 256 10 10 10 10 10 10 10 10 10
Sample Output
20 50 50 10 10 Myon
最近在学习acm的各种知识上各种混乱= =,然后基本上都没有做题,就算是敲出代码很多都不能输出正确的答案,但是还是要做题保持感觉啊。
这一题是在看关于贪心算法的ppt的时候作为题目讲的,然后自己把代码敲出来。
主要的思路就是从最后一天开始枚举到第一天,每天做尽可能少的食物。用一个tmp变量保存当前还需要做多少的食物。
一开始最外面的case的循环出了点小错误所以过不了= =,果然太久没做题就这样。
从最近的学习中悟道一点:很多时候都需要有逆向思维。这一点其实很容易就想到,但是想在实践的时候能像吃饭喝水那样简单的话还是要多实践并从中积累(好吧,我承认最近没做多少题了= =)
上代码:
#include <iostream> #include <stdio.h> #define MAX 1000+10 using namespace std; int main() { int i,j,n,tmp,a[MAX],b[MAX],c[MAX]; while(scanf("%d",&n)!=EOF) { tmp=0; for(i=0;i<n;i++) {scanf("%d",&a[i]);} for(j=0;j<n;j++) scanf("%d",&b[j]); for(i=n-1;i>=0;i--) { tmp+=a[i]; if(tmp>b[i]) { c[i]=b[i]; tmp-=b[i]; } else { c[i]=tmp; tmp=0; } } if(tmp) printf("Myon\n"); else { for(i=0;i<n;i++) { if(i) printf(" "); printf("%d",c[i]); } printf("\n"); } } return 0; } /* Sample Input 5 10 10 100 10 10 50 50 50 50 50 9 1 2 4 8 16 32 64 128 256 10 10 10 10 10 10 10 10 10 Sample Output 20 50 50 10 10 Myon */