Advertisement
mickypinata

CUBE-T119: COI Road Cube

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