mickypinata

IPST58: Wreak It Ralph

Nov 25th, 2020 (edited)
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e5;
  5.  
  6. int height[N + 1], dp[N + 1];
  7. int nBuilding, Q;
  8.  
  9. int main(){
  10.  
  11.     scanf("%d %d", &nBuilding, &Q);
  12.     for(int i = 1; i <= nBuilding; ++i){
  13.         scanf("%d", &height[i]);
  14.     }
  15.  
  16.     stack<int> stk;
  17.     dp[1] = 0;
  18.     stk.push(1);
  19.     for(int i = 2; i <= nBuilding; ++i){
  20.         while(!stk.empty() && height[stk.top()] >= height[i]){
  21.             stk.pop();
  22.         }
  23.         if(!stk.empty()){
  24.             dp[i] = max(height[i] - height[stk.top()], dp[stk.top()]);
  25.         } else {
  26.             dp[i] = 0;
  27.         }
  28.         stk.push(i);
  29.     }
  30.     sort(dp + 1, dp + nBuilding + 1);
  31.  
  32.     for(int q = 1; q <= Q; ++q){
  33.         int energy;
  34.         scanf("%d", &energy);
  35.         int idx = upper_bound(dp + 1, dp + nBuilding + 1, energy) - dp - 1;
  36.         cout << nBuilding - idx << "\n";
  37.     }
  38.  
  39.     return 0;
  40. }
  41.  
Add Comment
Please, Sign In to add comment