Advertisement
Guest User

fruitsv1.cpp

a guest
Dec 7th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 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; // 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; // 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.     for (int i=0; i<N; i++) {
  57.         if(C<i-K && i>C+K) {
  58.  
  59.             printf("Day %d's fruits expired.\n", C);// commentout
  60.             if (C+1<N) C++;
  61.             printf("Start eating day %d's fruits\n", C); //comment out
  62.         }
  63.         for(int j=0; j<E[i]; j++) {
  64.             if (G[C]==0) {
  65.                 printf("Day %d's fruits are finished.", C); //commentout
  66.                 if (C+1<N)
  67.                 {
  68.                 C++;
  69.                 printf("Start eating day %d's fruits\n", C); //comment out
  70.                 }
  71.                 else
  72.                 {
  73.                 printf("\n");
  74.                 break;
  75.                 }
  76.             }
  77.             if(G[C]>0) {
  78.                 G[C]--;
  79.                 printf("Ate one of Day %d's fruit\n", C); //commentout
  80.                 F++;
  81.             }
  82.         }
  83.         if (C+1>=N) break;
  84.         printf("Day %d is over\n", i); // commentout
  85.  
  86.     }
  87.     printf("%d", F);
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement