Advertisement
Tevronis

235

Mar 2nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <string>
  6. #include <map>
  7. #include <queue>
  8. #include <cmath>
  9. using namespace std;
  10.  
  11. typedef long long li;
  12. typedef pair<int, int> pii;
  13. vector<int> dp;
  14. vector<vector<int> > g;
  15. int getz(int x) {
  16. if (dp[x] != -1) {
  17. printf("\tЗначение для вершины %d вычисленои равно %d, выходим из рекурсии\n", x, dp[x]);
  18. return dp[x];
  19. }
  20.  
  21. if (x == 0)
  22. return dp[x] = 1;
  23. dp[x] = 1;
  24. for (int i = 0; i < g[x].size(); i++) {
  25. int to = g[x][i];
  26. printf("\tЗначение %d на текущем шаге: %d\n", x, dp[x]);
  27. printf("\tПытаемся обновить ответ для %d вершины\n", x);
  28.  
  29. printf("\tПереходим в соседа № %d\n", to);
  30.  
  31. dp[x] = max(dp[x], getz(to) + 1);
  32. }
  33. return dp[x];
  34.  
  35. }
  36.  
  37. int main() {
  38. #ifdef _DEBUG
  39. freopen("input.txt", "r", stdin);
  40. freopen("output.txt", "w", stdout);
  41. #endif
  42. int n, d;
  43. cin >> n >> d;
  44. vector<int> arr(n);
  45. dp.resize(n, -1);
  46. g.resize(n, vector<int>());
  47. for (int i = 0; i < n; i++) {
  48. cin >> arr[i];
  49. }
  50.  
  51. for (int i = n - 1; i >= 0; --i) {
  52. cout << "Вершина №: " << i << " Соседи: ";
  53. for (int j = i - 1; j >= 0; --j) {
  54. if (abs(arr[i] - arr[j]) <= d) {
  55. g[i].push_back(j);
  56. cout << j << " ";
  57. }
  58. }
  59. cout << endl;
  60. }
  61. int maxx = 0;
  62. for (int i = 0; i < n; i++) {
  63. printf("Запускаем динамику с %d вершины \n", i);
  64. maxx = max(getz(i), maxx);
  65. printf("Значение динамики для %d вершины, после прохода: %d\n", i, dp[i]);
  66. }
  67. cout << "Ответ: " << maxx << endl;
  68.  
  69.  
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement