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