Advertisement
smatskevich

Seminar1

Feb 13th, 2023
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <deque>
  2. #include <iostream>
  3. #include <unordered_map>
  4. #include <vector>
  5.  
  6. int S(int n, std::unordered_map<int, int>& cache) {
  7.   int& value = cache[n];
  8.   if (value != 0) return value;
  9.   return value = S(n - 3, cache) + S(n - 2, cache) + S(n - 1, cache);
  10. }
  11.  
  12. int main1() {
  13.   int n = 0; std::cin >> n;
  14.   std::unordered_map<int, int> cache{{0, 1}, {1, 1}, {2, 2}};
  15.   std::cout << S(n, cache) << std::endl;
  16.   return 0;
  17. }
  18.  
  19. int main2() {
  20.   int n = 0;
  21.   std::cin >> n;
  22.   std::vector<int> v(n + 1);
  23.   v[0] = v[1] = 0;
  24.   for (int i = 2; i <= n; ++i) {
  25.     v[i] = v[i - 1];
  26.     if (i % 2 == 0) v[i] = std::min(v[i], v[i / 2]);
  27.     if (i % 3 == 0) v[i] = std::min(v[i], v[i / 3]);
  28.     ++v[i];
  29.   }
  30.   std::cout << v[n] << std::endl;
  31.   return 0;
  32. }
  33.  
  34. int main3() {
  35.   int n, k; std::cin >> n >> k;
  36.   std::vector<int> x(k);
  37.   int phatka = 0;
  38.   for (int& value : x) {
  39.     std::cin >> value;
  40.     phatka = std::max(value, phatka);
  41.   }
  42.   std::deque<int> dp;
  43.   dp.push_back(0);
  44.   for (int i = 1; i <= n; ++i) {
  45.     dp.push_back(0);  // dp[i]
  46.     for (int j = 0; j < k; ++j) {
  47.       if (x[k] <= i) {
  48.       //  dp[i ?] = std::min(dp[i ?], dp[i - x[j] ?] + 1);
  49.       }
  50.     }
  51.     if (dp.size() > n - phatka) dp.pop_front();
  52.   }
  53.   return 0;
  54. }
  55.  
  56. struct Node {
  57.   int Parent = 0;
  58.   std::vector<int> Children;
  59. };
  60.  
  61. int main() {
  62.   int n = 0; std::cin >> n;
  63.   std::vector<Node> tree(n);
  64.   for (int i = 0; i < n - 1; ++i) {
  65.     int a, b; std::cin >> a >> b;
  66.     if (a > b) std::swap(a, b);
  67.     tree[b].Parent = a;
  68.     tree[a].Children.push_back(b);
  69.   }
  70.   for (int i = 0; i < n; ++i) {
  71.     std::cout << i << ": ";
  72.     for (int& x : tree[i].Children) std::cout << x << " ";
  73.     std::cout << std::endl;
  74.   }
  75.   return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement