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);
}
}