Advertisement
Guest User

fruitsv2.cpp

a guest
Dec 7th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.  
  7.  
  8.     // INPUT SECTION
  9.  
  10.  
  11.  
  12.     int N=0; // number of days
  13.  
  14.     scanf("%d", &N);
  15.  
  16.     int G[N+10];// array of fruits - all
  17.     int E[N+10];//number of fruits you can eat
  18.     int K=0; // day number that will expire - after day 1 the fruits to eat at day 0 will expire.
  19.     int Et=0; // total fruits can eat - for prevention of nest
  20.     // On day 1, day 0's fruits are still good when K=1.
  21.     // On day 2, day 0's fruits are still good when K=2.
  22.  
  23.     // Hence, on day K+n, day n's fruits are still good.
  24.  
  25.     scanf("%d", &K);
  26.  
  27.     for (int i=0; i<N; i++) { // i is day number from 0 to N-1;
  28.         scanf("%d", &G[i]); // input number of fruits each day.
  29.  
  30.     }
  31.  
  32.     for (int i=0; i<N; i++) {
  33.         scanf("%d", &E[i]); // number of fruits you can eat.
  34.         Et += E[i];
  35.     }
  36.  
  37.     // All done with the input, go through the output!
  38.  
  39.     // OUTPUT SECTION
  40.  
  41.     // Algorithm is to eat the fruits that expire first.
  42.     int F=0; // max fruits
  43.     int C=0; //day number can eat which starts at 0, notably i-K;
  44.  
  45.     // On day i=2, day 0's fruits expire when K=1.
  46.     // On day i=3, day 0's fruits expire when K=2.
  47.     // On day i, day i-K-1's fruits expire.
  48.     // input order is
  49.     // N K
  50.     // Array G
  51.     // Array E
  52.  
  53.     // Hence, increment to 1, when K=1, if C < 1, when i > 1.
  54.     // increment to 2 if C < 2, when K=1, if C < 2, when i > 2.
  55.     // increment C by 1 when day 0's expired, when K=1 and i=K+1.
  56.     int d=0; //day number
  57.     int status=0;
  58.     for (int i=0; i<Et; i++) { // i is fruit number
  59.         if(C<d-K && d>C+K) {
  60.  
  61.             printf("Day %d's fruits expired.\n", C);// commentout
  62.             if (C+1<N) C++;
  63.             printf("Start eating day %d's fruits\n", C); //comment out
  64.         }
  65.  
  66.         if (G[C]==0) {
  67.             printf("Day %d's fruits are finished.", C); //commentout
  68.             if (C+1<N) {
  69.                 C++;
  70.                 printf("Start eating day %d's fruits\n", C); //comment out
  71.             } else {
  72.                 printf("\n");
  73.                 status=1;
  74.                 break;
  75.             }
  76.         }
  77.         if (status==1) break;
  78.         if(G[C]>0) {
  79.             G[C]--;
  80.             printf("Ate one of Day %d's fruit\n", C); //commentout
  81.             F++;
  82.             E[d]--;
  83.         }
  84.  
  85.  
  86.         if (E[d]==0) {
  87.         printf("Day %d is over\n", d); // commentout
  88.         d++;
  89.         }
  90.     }
  91.     printf("%d", F);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement