Advertisement
TAImatem

Аврора Интенсив день3

Nov 24th, 2022
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.25 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define _USE_MATH_DEFINES
  3. #include "stdio.h"
  4. #include "stdlib.h"
  5. #include "time.h"
  6. #include <cmath>
  7. #include <math.h>
  8. #include <algorithm>
  9. #include <map>
  10. #include <vector>
  11. #include <utility>
  12. #include <set>
  13. #include <string>
  14. #include <cstring>
  15. #include <iostream>
  16. #include <fstream>
  17. #include <unordered_map>
  18. #include <unordered_set>
  19. #include <queue>
  20. #include <bitset>
  21. #include <cassert>
  22. #include <functional>
  23. //#include <intrin.h>
  24. #include <stack>
  25. #include <thread>
  26. using namespace std;
  27. //typedef long long ll;
  28. #define ll long long
  29. #define ld long double
  30.  
  31. const long long mod = 1000000007;
  32.  
  33. #define MIN(x,y) ((x)<(y)?(x):(y))
  34. #define MAX(x,y) ((x)>(y)?(x):(y))
  35. #define PI 3.14159265358979323846
  36. #define ABS(a) ((a)<0?-(a):(a))
  37. template <typename T> inline T gcd(T a, T b) {
  38.     while (b) { a %= b; swap(a, b); }
  39.     return a;
  40. }
  41. long long fastpow(long long a, long long n)
  42. {
  43.     auto mult = a;
  44.     long long res = 1;
  45.     while (n)
  46.     {
  47.         if (n & 1)
  48.             res *= mult;
  49.         mult *= mult;
  50.         n >>= 1;
  51.     }
  52.     return res;
  53. }
  54.  
  55. void BackPackMatr()
  56. {
  57.     int N, W;
  58.     cin >> N >> W;
  59.     vector<int> weights(N);
  60.     vector<vector<int>> backpack(N + 1);
  61.     backpack[0].resize(W, 0);
  62.     for (int i = 0; i < N; i++)
  63.     {
  64.         cin >> weights[i];
  65.         backpack[i + 1] = backpack[i];
  66.         backpack[i + 1][weights[i] - 1] = 1;
  67.         for (int j = weights[i] + 1; j <= W; j++)
  68.         {
  69.             backpack[i + 1][j - 1] = backpack[i][j - weights[i] - 1] || backpack[i][j - 1];
  70.         }
  71.     }
  72.     int i = N;
  73.     for (int j = W - 1; j >= 0; j--)
  74.     {
  75.         vector<int> ans;
  76.         if (backpack[i][j])
  77.         {
  78.             cout << j + 1 << " ";
  79.             while (j)
  80.             {
  81.                 while (backpack[i][j])
  82.                 {
  83.                     i--;
  84.                 }
  85.                 ans.push_back(weights[i]);
  86.                 j -= weights[i];
  87.             }
  88.             cout << ans.size() << endl;
  89.             for (auto& w : ans)
  90.                 cout << w << " ";
  91.         }
  92.     }
  93. }
  94.  
  95. void PricedBackpackMatr()
  96. {
  97.     int N, W;
  98.     cin >> N >> W;
  99.     vector<int> weights(N), costs(N);
  100.     vector<vector<long long>> backpack(N + 1);
  101.     backpack[0].resize(W + 1, -1);
  102.     backpack[0][0] = 0;
  103.     for (int i = 0; i < N; i++)
  104.     {
  105.         cin >> weights[i] >> costs[i];
  106.         backpack[i + 1] = backpack[i];
  107.         for (int j = weights[i]; j <= W; j++)
  108.         {
  109.             if (backpack[i][j - weights[i]] >= 0)
  110.                 backpack[i + 1][j] = max(backpack[i][j], backpack[i][j - weights[i]] + costs[i]);
  111.         }
  112.     }
  113.     int i = N;
  114.     int max = 0, maxj = 1;
  115.     for (int j = W; j >= 0; j--)
  116.     {
  117.         if (backpack[i][j] > max)
  118.         {
  119.             max = backpack[i][j];
  120.             maxj = j;
  121.         }
  122.     }
  123.     cout << maxj << " " << max;
  124.     int j = maxj;
  125.     vector<int> ans;
  126.     while (j)
  127.     {
  128.         while (backpack[i - 1][j] == backpack[i][j])
  129.         {
  130.             i--;
  131.         }
  132.         ans.push_back(i + 1);
  133.         j -= weights[i];
  134.     }
  135.     cout << ans.size() << endl;
  136.     for (auto& w : ans)
  137.         cout << w << " ";
  138. }
  139.  
  140. void BackPackColumn()
  141. {
  142.     int N, W, w;
  143.     cin >> N >> W;
  144.     vector<int> backpack(W, 0);
  145.     for (int i = 0; i < N; i++)
  146.     {
  147.         cin >> w;
  148.         backpack[w - 1] = 1;
  149.         for (int j = W; j >= w + 1; j--)
  150.         {
  151.             backpack[j - 1] = backpack[j - w - 1] || backpack[j - 1];
  152.         }
  153.     }
  154.     for (int j = W - 1; j >= 0; j--)
  155.     {
  156.         vector<int> ans;
  157.         if (backpack[j])
  158.         {
  159.             cout << j + 1 << endl;
  160.             return;
  161.         }
  162.     }
  163. }
  164.  
  165. long long cost(int i, int j, int a, vector<long long>& w, vector<long long>& h)
  166. {
  167.     return h[i] * w[i + a] * w[j + 1];
  168. }
  169.  
  170. void MinMatr()
  171. {
  172.     int n;
  173.     cin >> n;
  174.     vector<long long> w(n), h(n);
  175.     vector<vector<long long>> matr_res(n + 1);
  176.     for (int i = 0; i < n; i++)
  177.     {
  178.         cin >> h[i] >> w[i];
  179.     }
  180.     for (int i = 0; i < n + 1; i++)
  181.         matr_res[i].resize(n + 1, 0);
  182.     for (int ni = 0; ni < n - 1; ni++)
  183.     {
  184.         for (int nj = ni; nj < n - 1; nj++)
  185.         {
  186.             int i = nj - ni;
  187.             int j = nj;
  188.             long long min_res = INT64_MAX;
  189.             for (int a = 0; a <= ni; a++)
  190.             {
  191.                 min_res = min(min_res, matr_res[i + 1][i + a] + matr_res[i + a + 2][j + 1] + cost(i, j, a, w, h));
  192.             }
  193.             matr_res[i + 1][j + 1] = min_res;
  194.         }
  195.     }
  196.     cout << matr_res[1][n - 1] << endl;
  197. }
  198.  
  199. void LCS()
  200. {
  201.     string a, b;
  202.     cin >> a >> b;
  203.     vector<vector<int>> ans_matr(a.size() + 1);
  204.     ans_matr[0].resize(b.size() + 1, 0);
  205.     for (int i = 0; i < a.size(); i++)
  206.     {
  207.         ans_matr[i + 1].resize(b.size() + 1, 0);
  208.         for (int j = 0; j < b.size(); j++)
  209.             if (a[i] == b[j])
  210.                 ans_matr[i + 1][j + 1] = ans_matr[i][j] + 1;
  211.             else
  212.                 ans_matr[i + 1][j + 1] = max(ans_matr[i][j + 1], ans_matr[i + 1][j]);
  213.     }
  214.     cout << ans_matr[a.size()][b.size()] << endl;
  215.     string ans = "";
  216.     int i = a.size();
  217.     int j = b.size();
  218.     while (i && j)
  219.     {
  220.         while (i && ans_matr[i - 1][j] == ans_matr[i][j])
  221.             i--;
  222.         while (j && ans_matr[i][j - 1] == ans_matr[i][j])
  223.             j--;
  224.         if (i && j)
  225.         {
  226.             ans.push_back(a[i - 1]);
  227.             i--;
  228.             j--;
  229.         }
  230.     }
  231.     reverse(ans.begin(), ans.end());
  232.     cout << ans;
  233. }
  234.  
  235. void DFSMatr(vector<vector<bool>>& matr, vector<int>& passed, int i, int a)
  236. {
  237.     passed[i] = a;
  238.     for (int j = 0; j < matr[i].size(); j++)
  239.     {
  240.         if (matr[i][j] && !passed[j])
  241.         {
  242.             DFSMatr(matr, passed, i, a);
  243.         }
  244.     }
  245. }
  246.  
  247. void DFSList(vector<vector<int>>& matr, vector<int>& passed, int i, int a)
  248. {
  249.     passed[i] = a;
  250.     for (int j : matr[i])
  251.     {
  252.         if (matr[i][j] && !passed[j])
  253.         {
  254.             DFSList(matr, passed, i, a);
  255.         }
  256.     }
  257. }
  258.  
  259. void BFSMatr(vector<vector<bool>>& matr, vector<int>& passed, int i, int a)
  260. {
  261.     queue<int> que;
  262.     que.push(i);
  263.     passed[i] = a;
  264.     while (que.size())
  265.     {
  266.         int ti = que.front();
  267.         que.pop();
  268.         for (int j = 0; j < matr[ti].size(); j++)
  269.             if (matr[ti][j] && !passed[j])
  270.             {
  271.                 passed[j] = a;
  272.                 que.push(j);
  273.             }
  274.     }
  275.     vector<int> v;
  276.     int p = 0;
  277.     v.push_back(i);
  278.     while (p < v.size())
  279.     {
  280.         int ti = v[p];
  281.         p++;
  282.         for (int j = 0; j < matr[ti].size(); j++)
  283.             if (matr[ti][j] && !passed[j])
  284.             {
  285.                 passed[j] = a;
  286.                 v.push_back(j);
  287.             }
  288.     }
  289. }
  290.  
  291. void BFSList(vector<vector<int>>& matr, vector<int>& passed, int i, int a)
  292. {
  293.     queue<int> que;
  294.     que.push(i);
  295.     passed[i] = a;
  296.     while (que.size())
  297.     {
  298.         int ti = que.front();
  299.         que.pop();
  300.         for (int j : matr[ti])
  301.             if (matr[ti][j] && !passed[j])
  302.             {
  303.                 passed[j] = a;
  304.                 que.push(j);
  305.             }
  306.     }
  307.     vector<int> v;
  308.     int p = 0;
  309.     v.push_back(i);
  310.     while (p < v.size())
  311.     {
  312.         int ti = v[p];
  313.         p++;
  314.         for (int j : matr[ti])
  315.             if (matr[ti][j] && !passed[j])
  316.             {
  317.                 passed[j] = a;
  318.                 v.push_back(j);
  319.             }
  320.     }
  321. }
  322.  
  323. int main() {
  324. #ifdef DEBUG
  325.     freopen("input.txt", "r", stdin);
  326.     //freopen("output.txt", "w", stdout);
  327. #endif
  328.     return 0;
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement