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
题意:这个人每天要吃一定数量的食物,但是他每天也能采集一定数量的食物,但是他又想吃最新鲜的食物(其实就是最小值),问他每天要采集多少食物才不会被饿死。
题目比较水,稍加分析就行。
分析:从最后一天倒着往前走。第n天要采集的量是第n天要吃的量加上前一天没采集够的量。如果要采集的量小于这一天能采集的最大量,那么剩余的那部分就是0,否则向前累加。最后检查第一天要采集的量和他能采集的最大量就行。
#include<iostream> #include<string> #include<stdio.h> #include<string.h> using namespace std; int main() { int n,i,k,sum; int a[1010],b[1010],c[1010]; int max; while(~scanf("%d",&n)) { sum=0; memset(c,0,sizeof(c)); for(i=0;i<n;i++) { scanf("%d",&a[i]); if(max<a[i]) { k=i; max=a[i]; } } for(i=0;i<n;i++) { scanf("%d",&b[i]); } for(i=n-1;i>0;i--) { if(a[i]+sum<b[i])//就是这里是代码的核心。 { c[i]=a[i]+sum; sum=0; } else { sum=a[i]-b[i]+sum; c[i]=b[i]; } } if(a[0]+sum>b[0]) { cout<<"Myon"<<endl; } else { c[0]=a[i]+sum; for(i=0;i<n;i++) { if(i<n-1) cout<<c[i]<<" "; else { cout<<c[i]<<endl; } } } } }