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
Manager

题意不说了,有点麻烦。

POINT:

所要满足的条件为:

for all i1<i2, 都满足ci1,j ≥ ci2,j −k. 

那么当MIN[ci1,j]+k>=ci2,j 时,等式一定成立。

所以每次找最小值,比较就行。不满足了就break了。

#include <string>
#include <string.h>
#include <iostream>
#include <queue>
#include <cmath>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long LL;
#define lt x<<1
#define rt x<<1|1
const int maxn = 200200*4;
const int inf = 0x3f3f3f3f;
int a[maxn];
int num[maxn];
int sum[maxn];

void add(int x,int l,int r,int p)
{
	if(p==l&&l==r){
		sum[x]++;
	}else{
		int mid = (l+r)>>1;
		if(p<=mid) add(lt,l,mid,p);
		if(mid+1<=p) add(rt,mid+1,r,p);
		sum[x]=min(sum[lt],sum[rt]);
	}
}

int query(int x,int l,int r,int ll,int rr)
{
	int Min = inf;
	if(ll<=l&&rr>=r){
		Min = min(Min,sum[x]);
	}
	else{
		int mid = (l+r)>>1;
		if(ll<=mid) Min=min(Min,query(lt,l,mid,ll,rr));
		if(mid+1<=rr) Min=min(Min,query(rt,mid+1,r,ll,rr));
	}
	return Min;

}



int main()
{
	int n,k;
	while(~scanf("%d %d",&n,&k)){
		memset(sum,0,sizeof sum);
		memset(num,0,sizeof num);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			a[i]++;
		}
		int len = 0;
		for(int i=1;i<=n;i++){
			num[a[i]]++;
			add(1,1,n+1,a[i]);
			if(a[i]-1==0){
				len++;
				continue;
			}
			if(query(1,1,n+1,1,a[i]-1)+k<num[a[i]]){
				break;
			}
			len++;
		}
		printf("%d\n",len);
	}


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值