寧波市第32屆中小學生程式設計競賽(初中組) 母雞下蛋
阿新 • • 發佈:2019-01-23
問題 C: 母雞下蛋
雞國中的母雞最擅長下蛋了,MGMG 是雞國中一隻以下蛋產量高而聞名全雞國的母雞。
雞國專供下蛋的 n 個雞窩呈一字排列在雞國的“下蛋中心”,從左到右依次編號為 1 到n。每個雞窩都有一個最大可下蛋的量,其中第 i 個雞窩的最大可下蛋量為 ci 。有時候由於MGMG 產量實在太大而無法在一個雞窩中下完所有的蛋,不得不轉移到隔壁的雞窩繼續下蛋,如果隔壁的雞窩還是不能讓它下完所有的蛋,則 MGMG 繼續轉移,直到下完所有的蛋,或者向“下蛋中心”管理員投訴“雞窩數量實在太少了,我一隻雞的下蛋量都裝不下!”。
為了節省轉移時所耗費的體力,請你程式設計幫助 MGMG 找若干個連續的雞窩(個數儘量少),讓它能下完所有的蛋。
輸入
輸入共 2 行。
第 1 行輸入兩個整數 n 和 t,表示“下蛋中心”有 n 個可供下蛋的雞窩,MGMG 一次總共要下 t 個雞蛋。
第 2 行 n 個正整數 ci (1≤i≤n),依次表示第 i 個雞窩最大可下蛋量為 ci 個。
輸出
輸出 1 行一個整數或一個單詞。當輸出整數時表示讓 MGMG 下完所有的蛋至少需要幾個連續的雞窩。當 MGMG 用完所有的雞窩都無法下完所有的蛋時,MGMG 表示非常憤怒,輸出單詞“Angry”(不包含雙引號,注意大小寫)。
樣例輸入
5 4
1 2 1 2 3
樣例輸出
2
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <string> #include <stack> #define LL long long #define INF 0x7fffffff #define MAX 200010 #define PI 3.1415926535897932 #define E 2.718281828459045 using namespace std; LL t,n; LL a[1000005]; int main() { scanf("%lld%lld",&n,&t); LL sum=0,minn,start,endd; for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); sum+=a[i]; } if(sum<t) printf("Angry"); else { sum=0;start=1;endd=1; while(sum<t) { sum+=a[endd]; endd++; } minn=endd-start; while(sum>=t) { sum-=a[start]; start++; while(sum<t) { sum+=a[endd]; endd++; //endd到n+1時不能再繼續加了 否則進入死迴圈 if(endd==n+2) break; //正常到n+1 所以當endd到n+2時應該跳出迴圈 } minn=min(minn,endd-start); } printf("%lld",minn); } return 0; }