首页 > 其他分享 >ACdream 1427 Nice Sequence

ACdream 1427 Nice Sequence

时间:2022-11-09 18:39:01浏览次数:40  
标签:cnt return 1427 Sequence int sequence maxn ans ACdream


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


线段树

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=500005;
int n,k,a[maxn],f[2][maxn],cnt[maxn],N;

int work(int x,int y,int z)
{
if (x) return y>z?y:z;
else return y<z?y:z;
}

void insert(int x,int y,int z)
{
y+=N;
f[x][y]=z;
for (int i=(y>>1);i;i>>=1)
{
f[x][i]=work(x,f[x][i+i],f[x][i+i+1]);
}
}

int get(int x,int l,int r)
{
int ans;
if (r==0) return 0x7FFFFFF;
if (l==n+2) return -1;
if (x) ans=0; else ans=0x7FFFFFF;
for (l+=N-1,r+=N+1;l^r^1;l>>=1,r>>=1)
{
if (~l&1) ans=work(x,f[x][l^1],ans);
if ( r&1) ans=work(x,f[x][r^1],ans);
}
return ans;
}

int main()
{
int i;
while (~scanf("%d%d",&n,&k))
{
memset(f,0,sizeof(f));
memset(cnt,0,sizeof(cnt));
for (i=1;i<=n;i++) scanf("%d",&a[i]);
for (N=1;N<=n+1;N+=N);
for (i=1;i<=n;i++)
{
int x=a[i]+1;
int y=get(0,1,x-1);
int z=get(1,x+1,n+1);
cnt[x]++;
if (cnt[x]-k>y||cnt[x]+k<z) break;
insert(0,x,cnt[x]);
insert(1,x,cnt[x]);
}
printf("%d\n",i-1);
}
return 0;
}



标签:cnt,return,1427,Sequence,int,sequence,maxn,ans,ACdream
From: https://blog.51cto.com/u_15870896/5837713

相关文章

  • POJ 3458 Colour Sequence
    DescriptionWehaveapileofcardsconsistingof100cardsthatarecolouredonbothsides.Thereisafinitenumberofcolours(atmost26).Inadditionthe......
  • ACdream 1430 SETI
    Description   AmateurastronomersTomandBobtrytofindradiobroadcastsofextraterrestrialcivilizationsinthe air.Recentlytheyreceivedsomes......
  • ACdream 1429 Rectangular Polygon
    Description   Arectangularpolygonisapolygonwhoseedgesareallparalleltothecoordinateaxes.Thepolygon musthaveasingle,non-intersecting......
  • Great Sequence (CF2,C) (贪心)
    思路:最后转化成一个链,然后贪心地从链的一端处理即可! #include<bits/stdc++.h>usingnamespacestd;#defineM2000005#defineriregisterintlonglo......
  • POJ3061 Subsequence
    思路:尺取法注意本题目中所有的内容全部是证书,这就为我们维护了一个很好的单调性.考虑最暴力的\(\mathcalO(n^3)\)的做法,就是枚举起点,终点,然后分别求和.但是......
  • [数学记录]abc276G Count Sequences
    题意:对满足以下条件的序列计数,膜\(998244353\):\(0\leqa_0\leqa_1\cdots\leqa_n\leqm\)$a_i\not\equiva_j\pmod3$这题的难点在于发现它是简单题。想了......
  • Codeforces Subsequences
    题目:KarllikesCodeforcesandsubsequences.HewantstofindastringoflowercaseEnglishlettersthatcontainsatleastksubsequencescodeforces.Outofall......
  • CF1349F1 Slime and Sequences (Easy Version)
    linkSolution以前看到过,但是一直没有做......
  • 20221427第十周学习总结
    2022-2023-120221427《计算机基础与程序设计》第十周学习总结作业信息班级链接(2022-2023-1-计算机基础与程序设计)作业要求(2022-2023-1计算机基础与程......
  • 第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南)-L Bit Sequence
    题意给你两个数l,m,大小为m的数组a,求[0,l]之间满足以下条件的数x的个数:对于任何i输入[0,m-1],f(x+i)%2=a[i];f(k):代表k在二进制下1的个数m的范围<=100,l<=1e18,a[i]=0/1......