Advertisement
dmkozyrev

217.cpp

Jun 12th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <vector>
  2. #include <stdio.h>
  3. #include <algorithm>
  4.  
  5. int main() {
  6. //  Открываем файлы:
  7.     freopen("input.txt", "rt", stdin);
  8.     freopen("output.txt", "wt", stdout);
  9.      
  10. //  Читаем данные:
  11.     int count_trees;
  12.     scanf("%d", &count_trees);
  13.    
  14.     std::vector<int> w(count_trees), e(count_trees);
  15.      
  16.     for (int i = 0; i < count_trees; ++i)
  17.         scanf("%d %d", &w[i], &e[i]);
  18.      
  19.     int count_pos;
  20.     scanf("%d", &count_pos);
  21.          
  22.     std::vector<int> x(count_pos);
  23.    
  24.     for (auto & it : x)
  25.         scanf("%d", &it);
  26.      
  27. //  Решаем задачу двумерным ДП:  
  28.     std::vector<std::vector<int>> max(count_pos, std::vector<int>(count_trees, 1));
  29.          
  30.     for (int last_pos = 1; last_pos < count_pos; ++last_pos)
  31.         for (int last_tree = 0; last_tree < count_trees; ++last_tree)
  32.             for (int prev_pos = 0; prev_pos < last_pos; ++prev_pos)
  33.                 for (int prev_tree = 0; prev_tree < count_trees; ++prev_tree) {
  34.                     if (
  35.                         x[prev_pos] + e[prev_tree] <= x[last_pos] &&
  36.                         x[prev_pos] + w[last_tree] <= x[last_pos] &&
  37.                         max[last_pos][last_tree] < max[prev_pos][prev_tree]+1
  38.                     )
  39.                         max[last_pos][last_tree] = max[prev_pos][prev_tree]+1;
  40.                 }
  41.                      
  42. //  Выводим ответ: максимум среди элементов последнего столбца (последней клумбы):
  43.     printf("%d", *max_element(max.back().begin(), max.back().end()));
  44.      
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement