Guest User

fruitsv3.cpp

a guest
Dec 8th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 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.     while (Et>0) { // Et is remaining
  59.         if(C<d-K && d>C+K) {
  60.  
  61.             printf("Day %d's fruits expired.\n", C);// commentout
  62.             Et-=G[C];
  63.             if (C+1<N) C++;
  64.             printf("Start eating day %d's fruits\n", C); //comment out
  65.         }
  66.  
  67.  
  68.  
  69.  
  70.         if(G[C]<=E[d]) { // eat all if the fruits left is lesser than what you can eat.
  71.             printf("Ate %d (all) of Day %d's fruit(s)\n", G[C], C); //commentout
  72.             E[d]-=G[C];
  73.             Et-=G[C];
  74.             F+=G[C];
  75.             G[C]=0;
  76.             printf("Day %d's fruits are finished.", C); //commentout
  77.             if (C+1<N) {
  78.                 C++;
  79.                 printf("Start eating day %d's fruits\n", C); //comment out
  80.             } else {
  81.                 printf("\n");
  82.  
  83.                 break;
  84.             }
  85.             if (E[d]==0) d++;
  86.  
  87.  
  88.  
  89.         }
  90.         if (Et==0) {
  91.         printf("No more fruits!\n"); //commentout
  92.         break;
  93.         }
  94.  
  95.         if(G[C]>E[d]) { // eat just E[d]=2 remaining fruits for the day.
  96.             printf("Ate %d (remaining/next) of Day %d's fruit(s)\n", E[d], C); //commentout
  97.             G[C]-=E[d];
  98.             Et-=E[d];
  99.             F+=E[d];
  100.             E[d]=0;
  101.  
  102.             if (G[C]==0) {
  103.                 printf("Day %d's fruits are finished.", C); //commentout
  104.                 if (C+1<N) {
  105.                     C++;
  106.                     printf("Start eating day %d's fruits\n", C); //comment out
  107.  
  108.                 }    else {
  109.                     printf("\n");
  110.  
  111.                     break;
  112.                 }
  113.  
  114.  
  115.             }
  116.             d++;
  117.  
  118.         }
  119.         if (Et==0) {
  120.             printf("No more fruits!\n"); //commentout
  121.             break;
  122.         }
  123.  
  124.  
  125.     }
  126.     printf("%d", F);
  127. }
Advertisement
Add Comment
Please, Sign In to add comment