MiinaMagdy

11683 - Laser Sculpture

Sep 3rd, 2022 (edited)
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. /*
  2.  *
  3.  * problem link: https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&category=0&problem=2730&mosmsg=Submission+received+with+ID+27781398
  4.  *
  5.  * */
  6.  
  7. #include <bits/stdc++.h>
  8.  
  9. using namespace std;
  10.  
  11. #define ll long long
  12. #define endl '\n'
  13. #define sz(x) int(x.size())
  14. #define all(x) x.begin(), x.end()
  15.  
  16. int main() {
  17.     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  18.  
  19.     int h, w;
  20.     while (scanf("%d", &h), h) {
  21.         scanf("%d", &w);
  22.         int high[w + 1];
  23.         high[0] = h;
  24.         stack<int> stk;
  25.         stk.push(0);
  26.         int cnt = 0;
  27.         for (int i = 1; i <= w; i++) {
  28.             scanf("%d", &high[i]);
  29.             while (stk.size() && high[stk.top()] <= high[i]) stk.pop();
  30.             if (stk.size() && high[stk.top()] > high[i]) {
  31.                 cnt += high[stk.top()] - high[i];
  32.                 stk.pop();
  33.             }
  34.             stk.push(i);
  35.         }
  36.         cout << cnt << endl;
  37.     }
  38. }
  39.  
Tags: UVA CP3
Advertisement
Add Comment
Please, Sign In to add comment