Acdream 1427 nice sequence

Source: Internet
Author: User
Tags integer numbers

Nice sequencetime limit: 12000/6000 ms (Java/others) memory limit: 128000/64000 KB (Java/others) submitstatisticnext problemproblem description

Let us consider the sequence A1, A2 ,..., an of non-negative integer numbers. denote as ci, j the number of occurrences of the number I among A1, A2 ,..., AJ. we call the sequence K-nice if for all I1 <I2 and for all j the following condition is satisfied: CI1, J ≥ci2, J? K.

Given the sequence A1, A2,..., an and the number k, find its longest prefix that is k-nice.

Input the first line of the input file contains N and K (1 ≤ n ≤ 200 000, 0 ≤ k ≤ 200 000 ). the second line contains N integer numbers ranging from 0 to n. output output the greatest LSuch that the sequence A1, A2,..., Al is k-nice.Sample Input
10 10 1 1 0 2 2 1 2 2 32 01 0
Sample output
Sourceandrew stankevich contest 23 managermathlover

Question and code:

This question was written by teammates during the competition. Because optimization was required during the query, a line segment tree was added to him to maintain the minimum value of the interval.

I read this question today, and it is not difficult. The definition of it is a bit difficult, in fact, when we input an element x from left to right, to ensure that its left element 1-(x-1) if the number of occurrences is greater than or equal to X, locate the longest prefix.

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <set>#include <map>#include <queue>#include <string>#define maxn 200010#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;typedef long long  ll;int a[200005];struct segment{    int l,r;    int value;} son[maxn<<2];void PushUp(int rt){    son[rt].value=min(son[rt<<1].value,son[rt<<1|1].value);}void Build(int l,int r,int rt){    son[rt].l=l;    son[rt].r=r;    if(l==r)    {        son[rt].value=0;        return;    }    int m=(l+r)/2;    Build(lson);    Build(rson);    PushUp(rt);}void Update(int p,int rt){    if(son[rt].l==son[rt].r)    {        son[rt].value++;        return;    }    int m=(son[rt].l+son[rt].r)/2;    if(p<=m)        Update(p,rt<<1);    else        Update(p,rt<<1|1);    PushUp(rt);}int  Query(int l,int r,int rt){    if(son[rt].l==l&&son[rt].r==r)    {        return son[rt].value;    }    int ret=0;    int m=(son[rt].l+son[rt].r)/2;    if(r<=m)        ret=Query(l,r,rt<<1);    else if(l>m)        ret=Query(l,r,rt<<1|1);    else    {        ret=Query(lson);        ret=min(ret,Query(rson));    }    return ret;}int main(){    int n,k,x,ans=0;    bool flag=false;    scanf("%d%d",&n,&k);    Build(1,n+1,1);    for(int i=1;i<=n;i++)    {        scanf("%d",&x);        if(!flag)        {            x++;            a[x]++;            Update(x,1);            if(Query(1,x,1)<a[x]-k)            flag=true;            if(!flag) ans=i;        }    }    printf("%d\n",ans);    return 0;}

Acdream 1427 nice sequence

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: 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.