51nod-1686 K-wide interval (binary + ruler)

Source: Internet
Author: User

Topic Links:

Section K Range

Base time limit: 1 seconds space limit: 131072 KB

Defines the number of times the value of an interval appears for its majority.
The number of n is given, and the value of K is the number of the value after sorting all the intervals.

Input
The first row two numbers n and K (1<=n<=100000,k<=n* (n-1)/2) The second row n number, 0<= each number <2^31
Output
A number represents the answer.
Input example
4 21 2) 3 2
Output example
2

Test instructions

Ideas:

First, the arrays are separated into [1,n] number, the attention is equal;
The second half of the answer T,check the interval of the number of regions greater than or equal to T, and (can be set to a range of more than or equal to the range of the T)
Check by using the ruler method, in order to enumerate the left end of each interval I, and then find the minimum interval right end point, with the left end of the legal interval of the scheme number is n (r-1);
Complexity of O (NLOGN)

AC Code:

//#include <bits/stdc++.h>#include <vector>#include<iostream>#include<queue>#include<cmath>#include<map>#include<cstring>#include<algorithm>#include<cstdio>using namespacestd;#defineRiep (n) for (int i=1;i<=n;i++)#defineRIOP (n) for (int i=0;i<n;i++)#defineRJEP (n) for (int j=1;j<=n;j++)#defineRJOP (n) for (int j=0;j<n;j++)#defineMST (SS,B) memset (ss,b,sizeof (ss));typedefLong Longll;template<classT>voidRead (t&num) {    CharCH;BOOLf=false;  for(Ch=getchar (); ch<'0'|| Ch>'9'; f= ch=='-', ch=GetChar ());  for(num=0; ch>='0'&&ch<='9'; num=num*Ten+ch-'0', ch=GetChar ()); F&& (num=-num);}intstk[ -], Tp;template<classT> Inlinevoidprint (T p) {if(!p) {Puts ("0");return; }  while(p) stk[++ TP] = p%Ten, p/=Ten;  while(TP) Putchar (stk[tp--] +'0'); Putchar ('\ n');}ConstLL mod=1e9+7;Const DoublePi=acos (-1.0);ConstLL inf=1e10;Const intn=1e5+ the;intn,k;intA[n],flag[n];structnode{intA,id,ans;} Po[n];intCMP (node X,node y) {if(X.A==Y.A)returnx.id<y.id; returnx.a<y.a;}intcmp1 (node X,node y) {returnx.id<y.id;}intCheckintx) {MST (flag,0); LL ans=0; intnum=0; intR=0; Riep (n) { while(1)        {            intpos=Po[r].ans; if(pos!=-1)if(flag[pos]>=x| | R>n) Break; R++; POS=Po[r].ans; Flag[pos]++; } ans=ans+n-(R1); Flag[po[i].ans]--; }    if(ans>=k)return 1; return 0;}intMain () {read (n); read (k); Riep (n) Read (PO[I].A), Po[i].id=i; Sort (PO+1, po+n+1, CMP); po[0].ans=po[0].a=-1; Riep (n) {if(po[i].a!=po[i-1].A) po[i].ans=i; Elsepo[i].ans=po[i-1].ans; } Sort (PO+1, po+n+1, CMP1); intL=1, r=N;  while(l<=r) {intMid= (l+r) >>1; if(Check (mid)) l=mid+1; Elser=mid-1; } print (L-1); return 0;}

51nod-1686 K-wide interval (binary + ruler)

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.