a53

PlatouK_v3_OF

a53
Jan 20th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. unordered_map<int, map<int, int>> M;
  4. int cer, k, n, x, y, curr(1), s, smax, ind;
  5. const int DIM = 10000;
  6. char buff[DIM + 5];
  7. int poz;
  8. inline void read(int &numar)
  9. {
  10. numar = 0;
  11. while (buff[poz] < '0' || buff[poz] > '9')
  12. if (++poz == DIM)
  13. fread(buff, 1, DIM, stdin), poz = 0;
  14. while ('0' <= buff[poz] && buff[poz] <= '9')
  15. {
  16. numar = numar * 10 + buff[poz] - '0';
  17. if (++poz == DIM)
  18. fread(buff, 1, DIM, stdin), poz = 0;
  19. }
  20. }
  21. int main()
  22. {
  23. read(k), read(n), read(x);
  24. for (int i = 2; i <= n; ++i)
  25. {
  26. read(y);
  27. if (x == y)
  28. ++curr;
  29. else ++M[x][-curr], curr = 1;
  30. x = y;
  31. }
  32. read(cer);
  33. ++M[x][-curr];
  34. for (const auto& m : M)
  35. {
  36. x = m.first;
  37. curr = k;
  38. s = 0;
  39. for (const auto& p : m.second)
  40. {
  41. if (curr >= p.second)
  42. curr -= p.second, s -= p.first * p.second;
  43. else if (curr < p.second)
  44. s -= p.first * curr, curr = 0;
  45. if (!curr)
  46. break;
  47. }
  48. if ((s > smax) || (s == smax && x > ind))
  49. smax = s, ind = x;
  50. }
  51. if (cer == 1)
  52. cout << smax;
  53. else cout << ind;
  54. return 0;
  55. }
Add Comment
Please, Sign In to add comment