Advertisement
dmkozyrev

217.cpp

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