O (n) to enumerate all segments with a length of K, each violent transfer.
During the transfer, only one number is inserted from the end and one number is deleted from the front.
Calculate the current Max and Min values.
These operations are O (logn) with the Multiset (distinct element is rewritable.
1 #include<cstdio> 2 #include<set> 3 using namespace std; 4 multiset<int>S; 5 multiset<int>::iterator it; 6 int n,m,limit; bool goal; 7 int a[1000001]; 8 int main() 9 {10 scanf("%d%d%d",&n,&m,&limit);11 for(int i=1;i<=n;i++) scanf("%d",&a[i]);12 for(int i=1;i<=m;i++) S.insert(a[i]);13 it=S.end(); it--;14 if((*it)-(*S.begin())<=limit) puts("1"),goal=1;15 for(int i=2;i<=n-m+1;i++)16 {17 S.erase(S.find(a[i-1]));18 S.insert(a[m+i-1]);19 it=S.end(); it--;20 if((*it)-(*S.begin())<=limit) printf("%d\n",i),goal=1;21 }22 if(!goal) puts("NONE");23 return 0;24 }
[Ruler acquisition method] [Multiset] bzoj1342 [baltic2007] sound mute Problem