Advertisement
mickypinata

TOI12: Barrier

Aug 11th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. using lli = long long;
  7. using pli = pair<lli, int>;
  8.  
  9. int main(){
  10.  
  11.     int n, lim;
  12.     scanf("%d", &n);
  13.     scanf("%d", &lim);
  14.  
  15.     vector<lli> qs(n + 1, 0);
  16.     for(int i = 1; i <= n; ++i){
  17.         scanf("%lld", &qs[i]);
  18.         qs[i] += qs[i - 1];
  19.     }
  20.  
  21.     lli mx = -1e18;
  22.     int mnlen = lim;
  23.     vector<lli> dp(n + 1, 0);
  24.     deque<pli> q;
  25.     for(int i = 1; i <= n; ++i){
  26.         while(!q.empty() && q.back().first <= -qs[i - 1]){
  27.             q.pop_back();
  28.         }
  29.         q.push_back(make_pair(-qs[i - 1], i));
  30.         while(!q.empty() && q.front().second < i - lim + 1){
  31.             q.pop_front();
  32.         }
  33.         dp[i] = qs[i] + q.front().first;
  34.         if(dp[i] > mx){
  35.             mx = dp[i];
  36.             mnlen = i - q.front().second + 1;
  37.         } else if(dp[i] == mx){
  38.             mnlen = min(mnlen, i - q.front().second + 1);
  39.         }
  40.     }
  41.  
  42.     if(mx <= 0){
  43.         cout << 0 << "\n" << 0;
  44.     } else {
  45.         cout << mx << "\n" << mnlen;
  46.     }
  47.  
  48.     return 0;
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement