Advertisement
Josif_tepe

Untitled

Mar 9th, 2022
1,372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <cstring>
  7. #include <fstream>
  8. using namespace std;
  9. const int maxn = 505;
  10. int n, k;
  11. vector<int> v(maxn), rev(maxn);
  12. int one[maxn], two[maxn];
  13. int query(int i, int j, char c) {
  14.     if(i == 0) {
  15.         if(c == '1') {
  16.             return one[j];
  17.         }
  18.         else {
  19.             return two[j];
  20.         }
  21.     }
  22.     if(i > j) {
  23.         return 0;
  24.     }
  25.     if(c == '1') {
  26.         return one[j] - one[i - 1];
  27.     }
  28.     return two[j] - two[i - 1];
  29. }
  30. int main() {
  31.     ios_base::sync_with_stdio(false);
  32. //    ifstream cin("in.txt");
  33.     cin >> n >> k;
  34.      
  35.     int o = 0, t = 0;
  36.     for(int i = 0; i < n; i++) {
  37.         cin >> v[i];
  38.         if(v[i] == 1) {
  39.             o++;
  40.         }
  41.         else {
  42.             t++;
  43.         }
  44.         one[i] = o;
  45.         two[i] = t;
  46.     }
  47.     int ret = 0;
  48.     if(k <= 2) {
  49.         for(int i = 0; i < n; i++) {
  50.             for(int j = i + 1; j < n; j++) {
  51.                 for(int x = j; x > i; x--) {
  52.                     ret = max(ret, query(0, i - 1, '1') + query(x, j, '1') + query(i, x - 1, '2') + query(j + 1, n - 1, '2'));
  53.                 }
  54.             }
  55.         }
  56.         cout << ret << endl;
  57.         return 0;
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement