[bzoj3048] [Usaco2013 Jan] Cow Lineup

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.