ACdream1427-Nice Sequence

Nice Sequence

Time Limit: 4000/2000MS (Java/Others)  Memory Limit: 128000/64000KB (Java/Others)
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 a 1, a 2,..., a l is k-nice.
Sample Input
10 1
0 1 1 0 2 2 1 2 2 3
2 0
1 0
Sample Output
8
0
Source
Andrew Stankevich Contest 23
Manager
题目大意:给出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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值