At first, the face was crazy.
Later I thought of maintaining a left and right hand two pointers L and R. Represents the different kinds of digital <=k+1 species in [l,r] this paragraph.
It is clear that the leftmost, legal L increases with R and does not decrease.
By the way discretization, remember the number of different kinds of numbers to calculate the answer.
Time complexity O (n)
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intmaxn=1e5+233;7 structzs{intV,id;} A[MAXN];8 intMP[MAXN],SM[MAXN];9 inti,j,n,m,ans,num,k;Ten One intRaCharRx; AInlineintRead () { -Rx=getchar (), ra=0; - while((rx<'0'|| Rx>'9')) rx=GetChar (); the while(rx>='0'&&rx<='9') ra*=Ten, ra+=rx- -, Rx=getchar ();returnRA; - } - BOOLCMP (ZS A,zs b) {returna.v<B.V;} -InlinevoidAddintx) {num+=!sm[x]++;} +InlinevoidDelintx) {num-=!--sm[x];} - intMain () { +N=read (), k=read (); A for(i=1; i<=n;i++) A[i].v=read (), a[i].id=i; atSort (A +1, A +1+N,CMP);intCnt=0; - for(i=1; i<=n;mp[a[i].id]=cnt,i++) - if(a[i].v!=a[i-1].v| | i==1) cnt++; - intL=1, r=1; sm[mp[1]]++,num=ans=1; - for(r=2; r<=n;r++){ - Add (Mp[r]); in while(num>k+1) del (Mp[l]), l++; -ans=Max (Ans,sm[mp[r]]); to //printf ("%d--%d\n", l,r); + } -printf"%d\n", ans); the}
View Code
[bzoj3048] [Usaco2013 Jan] Cow Lineup