Advertisement
Guest User

Cuie

a guest
Oct 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. /*
  2. `-/oo+/- ``
  3. .oyhhhhhhyo.`od
  4. +hhhhyyoooos. h/
  5. +hhyso++oosy- /s
  6. .yoooossyyo:``-y`
  7. ..----.` ``.-/+:.`
  8. `````..-::/.
  9. `..```.-::///`
  10. `-.....--::::/:
  11. `.......--::////:
  12. `...`....---:::://:
  13. `......``..--:::::///:`
  14. `---.......--:::::////+/`
  15. ----------::::::/::///++:
  16. ----:---:::::///////////:`
  17. .----::::::////////////:-`
  18. `----::::::::::/::::::::-
  19. `.-----:::::::::::::::-
  20. ...----:::::::::/:-`
  21. `.---::/+osss+:`
  22. ``.:://///-.
  23. */
  24. #include <iostream>
  25. #include <cstdio>
  26. #include <algorithm>
  27. #include <cstring>
  28. #include <vector>
  29. #include <stack>
  30. #include <queue>
  31. #include <deque>
  32. #include <map>
  33. #include <cmath>
  34. #define INF 0x3f3f3f3f
  35. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  36. #define MAX(a, b) (((a) < (b)) ? (b) : (a))
  37.  
  38. using namespace std;
  39.  
  40. const int N = 500000;
  41.  
  42. int v[5 + N];
  43. deque <pair<int,int>> dq;
  44.  
  45. int main() {
  46. freopen("cuie.in", "r", stdin);
  47. freopen("cuie.out", "w", stdout);
  48. int n, k, i, p1, p2, cnt, lmax, rez, cntprev;
  49. scanf("%d%d", &n, &k);
  50. for(i = 1; i <= n; i++)
  51. scanf("%d", &v[i]);
  52. p1 = p2 = 1;
  53. cnt = 0;
  54. lmax = 1;
  55. rez = 0;
  56. cntprev = 0;
  57. dq.push_back({v[1], 1});
  58. while(p2 < n) {
  59. while(p2 < n && cnt <= k) {
  60. p2++;
  61. if(v[p2] < dq.front().first) {
  62. cnt += (dq.front().first - v[p2]) * (p2 - p1);
  63. dq.clear();
  64. dq.push_back({v[p2], p2});
  65. } else {
  66. while(dq.size() && v[p2] < dq.back().first) dq.pop_back();
  67. dq.push_back({v[p2], p2});
  68. cnt += v[p2] - dq.front().first;
  69. }
  70. if(lmax < p2 - p1) rez = cntprev;
  71. else if(lmax == p2 - p1) rez = MIN(rez, cntprev);
  72. lmax = MAX(lmax, p2 - p1);
  73. cntprev = cnt;
  74. }
  75. while(p1 <= p2 && cnt > k) {
  76. if(v[p1] > dq.front().first) cnt -= v[p1] - dq.front().first;
  77. else {
  78. int fr = dq.front().first;
  79. dq.pop_front();
  80. while(dq.size() && dq.front().second < p1) dq.pop_front();
  81. cnt -= (dq.front().first - fr) * (p2 - p1);
  82. }
  83. p1++;
  84. }
  85. if(lmax < p2 - p1 + 1) rez = cnt;
  86. else if(lmax == p2 - p1 + 1) rez = MIN(rez, cnt);
  87. lmax = MAX(lmax, p2 - p1 + 1);
  88. }
  89. printf("%d %d\n", lmax, rez);
  90. return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement