ACdream 1427 Nice Sequence(线段树)

题目链接: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

mathlover


题意:

    给定一序列,问符合要求的最长前缀的长度是多少。要求为:比某一位数值小的数值对应的个数+k要大于等于该数值,因此只要用线段树维护比当前数值小的数值对应的个数中的最小值即可(有点绕),若满足关系,则继续后推,不满足,则终止。线段树是按数值大小从小到大分段,每段维护的是,该区间的最小值。试着用数状数组也写了一遍,不知道为什么WA。


线段树(AC)代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
//ori原序列,num计数
int ori[200010],num[200010];
//线段树上维护该区间最小值
struct segTree
{
	int l,r,minn;
}tree[200010*4];
int min(int a,int b)
{
	return a<b?a:b;
}
//建树
void build(int i,int l,int r)
{
  tree[i].l=l;
  tree[i].r=r;
  tree[i].minn=0;
  if(tree[i].l==tree[i].r)
	return;
  int mid=(l+r)>>1;
  build(i<<1,l,mid);
  build(i<<1|1,mid+1,r);

}
//更新操作
void update(int p,int i)
{
  if(tree[i].l==tree[i].r&&tree[i].r==p)
  {
	  tree[i].minn++;
      return;
  }
  int mid=(tree[i].l+tree[i].r)>>1;
  if(p<=mid)
	  update(p,i<<1);
  else
	  update(p,i<<1|1);
  tree[i].minn=min(tree[i<<1].minn,tree[i<<1|1].minn);
}
//询问操作
int qury(int i,int l,int r)
{
  if(tree[i].l==l&&tree[i].r==r)
	  return tree[i].minn;
  int mid=(tree[i].l+tree[i].r)>>1;
  if(r<=mid)return qury(i<<1,l,r);
  else if(l>mid) return qury(i<<1|1,l,r);
  else return min(qury(i<<1,l,mid),qury(i<<1|1,mid+1,r));
}
int  main()
{
    int n,k,tmp,ans;
	while(~scanf("%d%d",&n,&k))
	{
		ans=0;
		memset(num,0,sizeof(num));
		for(int i=0;i<n;i++)
			scanf("%d",&ori[i]);
		build(1,0,n);
		for(int i=0;i<n;i++)
		{
			num[ori[i]]++;
			update(ori[i],1);			
			if(ori[i]==0)
			{
				ans=i+1;
				continue;
			}
		    tmp=qury(1,0,ori[i]-1);
			//满足则后移
			if(tmp+k>=num[ori[i]])
			{
				ans=i+1;
			}
			else
				break;
		}
		printf("%d\n",ans);
	}
	return 0;
}

数状数组(WA)代码:

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
//ori原序列,idx区间最小值,num计数
int num[200010],idx[200010],ori[200010]; 
//前移多少位
int Lowbit(int x)
{
	return x&(-x);
}
int min(int a,int b)
{
	return a<b?a:b;
}
//初始化
void Init(int n)
{
     for(int i=1;i<=n;i++)
	 {
          idx[i]=num[i];
          for(int j=1;j<Lowbit(i);j<<=1)
		  {
               idx[i]=min(idx[i],idx[i-j]);
          }
     }
}
//询问操作
int Query(int l,int r)
{
     int ans=num[r];
     while(true)
	 {
          ans=min(ans,num[r]);
          if(r==l) break;
          for(r-=1;r-l>=Lowbit(r);r-=Lowbit(r)){
               ans=min(ans,idx[r]);
          }
     }
     return ans;
}
//更新操作
void Modify(int p,int v,int n)
{
    num[p]=num[p]+1;
	v=num[p];
	//外层循环向后更新
    for(int i=p;i<=n;i+=Lowbit(i))
	{
        idx[i]=v;
        for(int j=1;j<Lowbit(i);j<<=1){
            idx[i]=min(idx[i],idx[i-j]);
        }
    }
} 
int main()
{
    int N,k,res=0,tmp;
	while(scanf("%d%d",&N,&k)!=EOF)
	{
	    memset(num,0,sizeof(num));
		memset(idx,0,sizeof(idx));
		res=0;
		for(int i=0;i<N;i++)
		{
			scanf("%d",&ori[i]);
			ori[i]++;
		}
		for(int i=0;i<N;i++)
		{
			if(ori[i]==1)
			{
               res=i+1;
			   Modify(ori[i],1,N+1);
			   continue;
			}
			tmp=Query(1,ori[i]-1);
			Modify(ori[i],1,N+1);
			if(tmp+k>=num[ori[i]])
			{
				res=i+1;
			}
			else
			   break;
		}
		printf("%d\n",res);
	}
	return 0;
}


你可能感兴趣的:(线段树)