-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlidingWindowOptimum.cpp
60 lines (54 loc) · 1.01 KB
/
SlidingWindowOptimum.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;
int a[100];
int f[11];
int optLsubK(int n,int k)
{
int x=0,y=0;
int res=0;
int max_r=-1,l=0,cr=k;
for(int r=0;r<n;r++)
{
if(cr==0)
{
if(f[a[r]]>0)
{
f[a[r]]++;
max_r++;
}
else
{
r--;
if(res<=(max_r-l+1))
{
y=max_r;
x=l;
res=(max_r-l+1);
}
f[a[l]]--;
if(f[a[l]]==0)
{
cr++;
}
l++;
}
}
else
{
f[a[r]]++;
max_r++;
cr--;
}
}
cout<<x<<" "<<y<<endl;
return res;
}
int main()
{
int n,k;
cin>>n>>k;
for(int i=0;i<n;i++)
cin>>a[i];
cout<<optLsubK(n,k);
return 0;
}