Beingamanforever

De Shaw Q

Nov 13th, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-11-13 17:00:06
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25. void solve()
  26. {
  27.     int n, val;
  28.     cin >> n >> val;
  29.     vi arr(n);
  30.     int cnt_val = 0;
  31.     for (auto &x : arr)
  32.     {
  33.         cin >> x;
  34.         if (x == val)
  35.         {
  36.             cnt_val++;
  37.         }
  38.     }
  39.     // TODO: Split the array into segments separated by val
  40.     // and find the maximum frequency of any element in any segment
  41.     int maxcnt = 0;
  42.     map<int, int> freq;
  43.     for (int i = 0; i < n; i++)
  44.     {
  45.         if (arr[i] == val)
  46.         {
  47.             freq.clear();
  48.         }
  49.         else
  50.         {
  51.             freq[arr[i]]++;
  52.             if (freq[arr[i]] > maxcnt)
  53.             {
  54.                 maxcnt = freq[arr[i]];
  55.             }
  56.         }
  57.     }
  58.     int final = cnt_val + maxcntt;
  59.     cout << final << endl;
  60.     return;
  61. }
  62.  
  63. signed main()
  64. {
  65.     NeedForSpeed;
  66.     int t = 1;
  67.     cin >> t;
  68.     while (t--)
  69.     {
  70.         solve();
  71.     }
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment