-
Notifications
You must be signed in to change notification settings - Fork 1
/
1093.cpp
38 lines (37 loc) · 999 Bytes
/
1093.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
ll diff, t, i, n, a[100005], ms, j;
deque <ll> big, small;
cin >> t;
for(i=1; i<=t; i++)
{
cin >> n;
cin >> ms;
for(j=0; j<n; j++)
cin >> a[j];
big.clear();
small.clear();
big.push_back(0);
small.push_back(0);
diff=0;
for(j=1; j<n; j++)
{
while(!big.empty() && a[big.back()]<=a[j])
big.pop_back();
big.push_back(j);
while(j-big.front()>=ms)
big.pop_front();
while(!small.empty() && a[small.back()]>=a[j])
small.pop_back();
small.push_back(j);
while(j-small.front()>=ms)
small.pop_front();
diff=max(diff, a[big.front()]-a[small.front()]);
}
printf("Case %lld: %lld\n", i, diff);
}
return 0;
}