Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- // INPUT SECTION
- int N=0; // number of days
- scanf("%d", &N);
- int G[N+10];// array of fruits - all
- int E[N+10];//number of fruits you can eat
- int K=0; // day number that will expire - after day 1 the fruits to eat at day 0 will expire.
- int Et=0; // total fruits can eat - for prevention of nest
- // On day 1, day 0's fruits are still good when K=1.
- // On day 2, day 0's fruits are still good when K=2.
- // Hence, on day K+n, day n's fruits are still good.
- scanf("%d", &K);
- for (int i=0; i<N; i++) { // i is day number from 0 to N-1;
- scanf("%d", &G[i]); // input number of fruits each day.
- }
- for (int i=0; i<N; i++) {
- scanf("%d", &E[i]); // number of fruits you can eat.
- Et += E[i];
- }
- // All done with the input, go through the output!
- // OUTPUT SECTION
- // Algorithm is to eat the fruits that expire first.
- int F=0; // max fruits
- int C=0; //day number can eat which starts at 0, notably i-K;
- // On day i=2, day 0's fruits expire when K=1.
- // On day i=3, day 0's fruits expire when K=2.
- // On day i, day i-K-1's fruits expire.
- // input order is
- // N K
- // Array G
- // Array E
- // Hence, increment to 1, when K=1, if C < 1, when i > 1.
- // increment to 2 if C < 2, when K=1, if C < 2, when i > 2.
- // increment C by 1 when day 0's expired, when K=1 and i=K+1.
- int d=0; //day number
- int status=0;
- for (int i=0; i<Et; i++) { // i is fruit number
- if(C<d-K && d>C+K) {
- printf("Day %d's fruits expired.\n", C);// commentout
- if (C+1<N) C++;
- printf("Start eating day %d's fruits\n", C); //comment out
- }
- if (G[C]==0) {
- printf("Day %d's fruits are finished.", C); //commentout
- if (C+1<N) {
- C++;
- printf("Start eating day %d's fruits\n", C); //comment out
- } else {
- printf("\n");
- status=1;
- break;
- }
- }
- if (status==1) break;
- if(G[C]>0) {
- G[C]--;
- printf("Ate one of Day %d's fruit\n", C); //commentout
- F++;
- E[d]--;
- }
- if (E[d]==0) {
- printf("Day %d is over\n", d); // commentout
- d++;
- }
- }
- printf("%d", F);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement