ACdream1427-Nice Sequence

来源:互联网 发布:noppoo choc mac 编辑:程序博客网 时间:2024/06/11 12:47

Nice Sequence

Time Limit: 4000/2000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
Submit Statistic Next Problem

Problem 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 l such 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

80

Source

Andrew Stankevich Contest 23

Manager

mathlover
题目大意:给出n,k。n表示长度为n的序列,且序列中的值都是0---n的。定义ci,j表示数字i在a1,a2...aj中出现的次数,定义k-nice序列,即所有i1<i2且满足不等式ci1,j ≥ ci2,j −k。

解题思路:用sum[i]记录当前i出现的次数。每判断一个数a进来,sum[a]++。只要sum[1]...sum[a-1]都满足sum[i]+K>=summ[a]就可以了,一旦不满足就结束程序并输出答案。那么找sum[0]...sum[a-1]中最小的即可。如果最小的都满足不等式就不用验证其他的了。用线段树实现这个查询操作。

#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;const int INF=0x3f3f3f3f;int sum[200100],n,k;struct node{    int l,r,val;}tree[200100*4];void build(int k,int l,int r){    tree[k].l=l;    tree[k].r=r;    tree[k].val=0;    if(l==r) return;    int mid=(l+r)>>1;    build(k<<1,l,mid);    build(k<<1|1,mid+1,r);}void add(int k,int x){    if(tree[k].l==tree[k].r)    {        tree[k].val++;        return;    }    int mid=(tree[k].l+tree[k].r)>>1;    if(x<=mid) add(k<<1,x);    else add(k<<1|1,x);    tree[k].val=min(tree[k<<1].val,tree[k<<1|1].val);}int f(int k,int x){    if(tree[k].r<=x) return tree[k].val;    int mid=(tree[k].l+tree[k].r)>>1;    int x1=f(k<<1,x),x2=INF;    if(mid<x) x2=f(k<<1|1,x);    return min(x1,x2);}int main(){    int x[200090];    while(~scanf("%d %d",&n,&k))    {        memset(sum,0,sizeof sum);        build(1,0,n-1);        for(int i=1;i<=n;i++)            scanf("%d",&x[i]);        int ans=0;        for(int i=1; i<=n; i++)        {            sum[x[i]]++;            add(1,x[i]);            if(f(1,x[i])+k>=sum[x[i]]) ans=i;            else break;        }        printf("%d\n",ans);    }    return 0;}
0 0