Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- `-/oo+/- ``
- .oyhhhhhhyo.`od
- +hhhhyyoooos. h/
- +hhyso++oosy- /s
- .yoooossyyo:``-y`
- ..----.` ``.-/+:.`
- `````..-::/.
- `..```.-::///`
- `-.....--::::/:
- `.......--::////:
- `...`....---:::://:
- `......``..--:::::///:`
- `---.......--:::::////+/`
- ----------::::::/::///++:
- ----:---:::::///////////:`
- .----::::::////////////:-`
- `----::::::::::/::::::::-
- `.-----:::::::::::::::-
- ...----:::::::::/:-`
- `.---::/+osss+:`
- ``.:://///-.
- */
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <vector>
- #include <stack>
- #include <queue>
- #include <deque>
- #include <map>
- #include <cmath>
- #define INF 0x3f3f3f3f
- #define MIN(a, b) (((a) < (b)) ? (a) : (b))
- #define MAX(a, b) (((a) < (b)) ? (b) : (a))
- using namespace std;
- const int N = 500000;
- int v[5 + N];
- deque <pair<int,int>> dq;
- int main() {
- freopen("cuie.in", "r", stdin);
- freopen("cuie.out", "w", stdout);
- int n, k, i, p1, p2, cnt, lmax, rez, cntprev;
- scanf("%d%d", &n, &k);
- for(i = 1; i <= n; i++)
- scanf("%d", &v[i]);
- p1 = p2 = 1;
- cnt = 0;
- lmax = 1;
- rez = 0;
- cntprev = 0;
- dq.push_back({v[1], 1});
- while(p2 < n) {
- while(p2 < n && cnt <= k) {
- p2++;
- if(v[p2] < dq.front().first) {
- cnt += (dq.front().first - v[p2]) * (p2 - p1);
- dq.clear();
- dq.push_back({v[p2], p2});
- } else {
- while(dq.size() && v[p2] < dq.back().first) dq.pop_back();
- dq.push_back({v[p2], p2});
- cnt += v[p2] - dq.front().first;
- }
- if(lmax < p2 - p1) rez = cntprev;
- else if(lmax == p2 - p1) rez = MIN(rez, cntprev);
- lmax = MAX(lmax, p2 - p1);
- cntprev = cnt;
- }
- while(p1 <= p2 && cnt > k) {
- if(v[p1] > dq.front().first) cnt -= v[p1] - dq.front().first;
- else {
- int fr = dq.front().first;
- dq.pop_front();
- while(dq.size() && dq.front().second < p1) dq.pop_front();
- cnt -= (dq.front().first - fr) * (p2 - p1);
- }
- p1++;
- }
- if(lmax < p2 - p1 + 1) rez = cnt;
- else if(lmax == p2 - p1 + 1) rez = MIN(rez, cnt);
- lmax = MAX(lmax, p2 - p1 + 1);
- }
- printf("%d %d\n", lmax, rez);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement