ACdream - 1427 - 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


题意:

一个数列有n个数a1, a2, ... , an ,Ci , j 表示 在 a1, a2, .. , aj 中 数i 出现的次数 (题目没有说明 ,不过i应该是>=0的)

求最长的k-nice序列

k-nice 序列就是对任意i1 < i2 均有Ci1 , j >= Ci2 , j-k

j即为该k-nice序列的长度


思路:

就是在数i前出现最少的数也不能Ci,j - k少

利用线段树求区间最小值


#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define LL long long
#define lx x<<1
#define rx x<<1|1
#define mid ((L+R)>>1)
const int N = 2e5 + 10;
int n,m,k,a[N],mi[N<<2];
void update(int x,int pos,int L,int R){
    if(L==R){
        mi[x]++;
        return ;
    }
    if(pos<=mid) update(lx, pos, L, mid);
    else update(rx, pos, mid+1, R);
    mi[x] = min(mi[lx], mi[rx]);
}
int Min(int x,int l,int r,int L,int R){
    if(l<=L&&R<=r) return mi[x];
    int ans = n+1;
    if(l<=mid) ans = Min(lx, l, r, L, mid);
    if(mid<r) ans = min(ans, Min(rx, l, r, mid+1, R));
    return ans;
}
int main()
{
    while(scanf("%d%d",&n,&k)!=EOF){
        int ans = -1;
        memset(a,0,sizeof a);
        for(int i=0;i<n;i++){
            scanf("%d",&m);
            if(ans!=-1) continue;
            a[m]++;
            update(1, m, 0, n);
            if(a[m]-k>Min(1, 0, m, 0, n)) ans = i;
        }
        if(ans==-1) ans = n;
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值