Th3NiKo

Wieża

May 14th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stack>
  4.  
  5. using namespace std;
  6.  
  7. struct Bucket{
  8. public:
  9.     int height;
  10.     int radius;
  11.     int ground;
  12. };
  13.  
  14. int main()
  15. {
  16.     int ile;
  17.     scanf("%d", &ile);
  18.     //cin >> ile;
  19.     for(int c = 0; c < ile; c++){
  20.     stack<Bucket> tower;
  21.     int hightestPoint = 0;
  22.     int bucketCount;
  23.     scanf("%d", &bucketCount);
  24.     Bucket temp;
  25.     Bucket temporary; //Used to speed up a bit
  26.     int maksimum;
  27.     int sum;
  28.     int counter = 0; //Its size of the Stack. Used it because thought its faster then empty() function
  29.     for(int i = 0; i < bucketCount; i++){
  30.         scanf("%d", &temp.radius);
  31.         scanf("%d", &temp.height);
  32.         maksimum = -1;
  33.         sum = 0;
  34.         if(counter == 0){
  35.             temp.ground = 0;
  36.             tower.push(temp);
  37.             counter++;
  38.         } else {
  39.             if(temp.radius < tower.top().radius){ //If bucket is narrower then push it
  40.                 temp.ground = tower.top().ground;
  41.                 tower.push(temp);
  42.                 counter++;
  43.             } else { //If bucket is wider
  44.                 while((counter > 0) && temp.radius >= (temporary = tower.top()).radius){ //Pop and search for new ground (maximum)
  45.                     sum = temporary.height + temporary.ground;
  46.                     if(maksimum < sum){
  47.                         maksimum = sum;
  48.                     }
  49.                     tower.pop();
  50.                     counter--;
  51.                 }
  52.                 temp.ground = maksimum; //Set ground for new bucket
  53.                 tower.push(temp);
  54.                 counter++;
  55.             }
  56.         }
  57.         sum = temp.height + temp.ground; //Meantime find highest point in stack
  58.         if(hightestPoint < sum){
  59.             hightestPoint = sum;
  60.         }
  61.     }
  62.     printf("%d \n", hightestPoint);
  63.     }
  64.  
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment