Advertisement
smatskevich

Seminar2

Jan 8th, 2022
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <map>
  4. #include <set>
  5. #include <string>
  6. #include <tuple>
  7. #include <unordered_map>
  8. #include <unordered_set>
  9. #include <vector>
  10.  
  11. typedef long long ll;
  12. using namespace std;
  13.  
  14. // 1 4 5 7 8
  15. // 2 3 4 5 6 8 9 10 12 140
  16.  
  17. // 4 5 8
  18.  
  19. vector<int> Intersection(vector<int>& a, vector<int>& b) {
  20.   vector<int> result;
  21.  
  22.   int i = 0, j = 0;
  23.   while (i < a.size() && j < b.size()) {
  24.     if (a[i] < b[j]) ++i;
  25.     else if (a[i] > b[j]) ++j;
  26.     else {
  27.       result.push_back(a[i]);
  28.       ++i; ++j;
  29.     }
  30.   }
  31.   return result;
  32. }
  33.  
  34. int main1() {
  35.   ios::sync_with_stdio(false);
  36.   cin.tie(nullptr);
  37.  
  38.   int n = 0;
  39.   cin >> n;
  40.   vector<int> a(n);
  41.   for (int i = 0; i < n; ++i) cin >> a[i];
  42.   int m = 0;
  43.   cin >> m;
  44.   vector<int> b(m);
  45.   for (int i = 0; i < m; ++i) cin >> b[i];
  46.  
  47.   vector<int> result = Intersection(a, b);
  48.  
  49.   for (int x : result) cout << x << " ";
  50.  
  51.   return 0;
  52. }
  53.  
  54. // 2 4 6 7 7 7 7 9 10 22 34
  55. int main2() {
  56.   ios::sync_with_stdio(false);
  57.   cin.tie(nullptr);
  58.  
  59.   int n = 0;
  60.   cin >> n;
  61.   int* b = new int[n];
  62.   vector<int> a(n);
  63.   for (int i = 0; i < n; ++i) { int v = 0; cin >> v; a[i] = v; b[i] = v; }
  64.   int x = 0;
  65.   cin >> x;
  66.  
  67.   auto position = lower_bound(a.begin(), a.end(), x, std::less<>());
  68.   if (position != a.end() && *position == x) cout << "YES" << endl;
  69.   else cout << "NO" << endl;
  70.  
  71.   auto position2 = lower_bound(b, b + n, x);
  72.   if (position2 != b + n && *position2 == x) cout << "YES" << endl;
  73.   else cout << "NO" << endl;
  74.  
  75.   delete[] b;
  76.   return 0;
  77. }
  78.  
  79. int main3() {
  80.   ios::sync_with_stdio(false);
  81.   cin.tie(nullptr);
  82.  
  83.   int n = 0;
  84.   cin >> n;
  85.   vector<int> a(n);
  86.   for (int i = 0; i < n; ++i) cin >> a[i];
  87.  
  88.   // 2 4 3 2 1
  89.   int l = 1, r = n;
  90.   while (l < r) {
  91.     int mid = (r + l) / 2;
  92.     if (a[mid] > a[mid - 1]) l = mid + 1;
  93.     else r = mid;
  94.   }
  95.   cout << l - 1;
  96.  
  97.   return 0;
  98. }
  99.  
  100. int main() {
  101.   ios::sync_with_stdio(false);
  102.   cin.tie(nullptr);
  103.  
  104.   int n = 0;
  105.   cin >> n;
  106.   vector<int> a(n);
  107.   for (int i = 0; i < n; ++i) cin >> a[i];
  108.   int m = 0;
  109.   cin >> m;
  110.  
  111.   // 2 4 3 2 1
  112.   int l = 0, r = m;
  113.   ll sum = 0;
  114.   for (int i = l; i < r; ++i) sum += a[i];
  115.  
  116.   ll max_sum = sum;
  117.   for (; r < n; ++l, ++r) {
  118.     sum += a[r];
  119.     sum -= a[l];
  120.     max_sum = max(sum, max_sum);
  121.   }
  122.   cout << max_sum;
  123.   return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement