ACdream 1427 Nice Sequence

ACdream 1427 Nice Sequence

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

主题链接:http://115.28.76.232/problem?

pid=1427

Nice Sequence

Time Limit: 12000/6000MS (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

用线段树维护 0到A[i]-1间的最小值。用F[A[i]] 统计频率。推断 0 到 A[i]-1范围内的最小值与F[A[i]]-K的大小就可以。

//#pragma comment(linker, "/STACK:36777216")
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
#define LL long long
#define MAXN 200100
struct Node{
    int left,right,v;
};Node Tree[MAXN<<2];
void Build(int id,int left,int right){
    Tree[id].v=0;
    Tree[id].left=left;
    Tree[id].right=right;
    if(left==right) return;
    int mid=(left+right)>>1;
    Build(id<<1,left,mid);
    Build(id<<1|1,mid+1,right);
}
void Update(int id,int pos,int add){
    int left=Tree[id].left,right=Tree[id].right;
    if(left==right){
        Tree[id].v+=add;
        return;
    }
    int mid=(left+right)>>1;
    if(mid>=pos)
        Update(id<<1,pos,add);
    else
        Update(id<<1|1,pos,add);
   Tree[id].v=min(Tree[id<<1].v,Tree[id<<1|1].v);
}
int Query(int id,int Qleft,int Qright){
    int left=Tree[id].left,right=Tree[id].right;
    if(left>=Qleft && right<=Qright)
        return Tree[id].v;
    int mid=(left+right)>>1;
    if(mid>=Qright)
        return Query(id<<1,Qleft,Qright);
    else if(mid<Qleft)
        return Query(id<<1|1,Qleft,Qright);
    int r1=Query(id<<1,Qleft,Qright),r2=Query(id<<1|1,Qleft,Qright);
    return min(r1,r2);;
}
int A[MAXN];
int F[MAXN];
int main(){
    int N,K;
    while(~scanf("%d%d",&N,&K)){
        for(int i=1;i<=N;i++)
            scanf("%d",&A[i]);
        Build(1,0,N);
        memset(F,0,sizeof(F));
        int i;
        for(i=1;i<=N;i++){
            F[A[i]]++;
            Update(1,A[i],1);
            if(A[i]==0)    continue;
            int ans=Query(1,0,A[i]-1);
            if(ans<F[A[i]]-K) break;
        }
        printf("%d\n",i-1);
    }
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117309.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • MATLAB中的norm函数

    MATLAB中的norm函数MATLAB 中的 norm 函数

    2025年7月24日
    3
  • git回退版本命令

    git回退版本命令如果你在本地做了错误提交 那么回退版本的方法很简单 1 先用下面命令找到要回退的版本的 commitid gitreflog2 接着回退版本 gitresethard 就是你要回退的版本的 commitid 的前面几位 远程分支版本回退的方法如果你的错误提交已经推送到自己的远程分支了 那么就需要回滚远程分支了 1 首先要回退本地分支 gitrefloggit 紧接着强制推送到远程分支 gi

    2025年6月19日
    4
  • Python使用captcha制作验证码[通俗易懂]

    Python使用captcha制作验证码[通俗易懂]在使用Keras搭建验证码识别模型时,需要大量的验证码图片。在这里,使用captcha模块生成验证码图片,验证码图片名称为验证码上显示的字符串。1生成单张验证码图片并显示生成一张验证码的代码如下:fromcaptcha.imageimportImageCaptchaimportmatplotlib.pyplotaspltimportnumpyasnpimport……

    2022年9月20日
    4
  • 唯一约束和唯一索引区别是什么_db2违反唯一索引的约束

    唯一约束和唯一索引区别是什么_db2违反唯一索引的约束1.主键约束(PRIMARY KEY)1)主键用于唯一地标识表中的每一条记录,可以定义一列或多列为主键。2)是不可能(或很难)更新.3)主键列上没有任何两行具有相同值(即重复值),不允许空(NULL).4)主健可作外健,唯一索引不可;2.唯一性约束(UNIQUE)1)唯一性约束用来限制不受主键约束的列上的数据的唯一性,用于作为访问某行的可选手段

    2022年9月16日
    3
  • spdlog学习笔记

    spdlog学习笔记说明:所有内容翻译自spdlog的wiki,受英语水平所限,有所错误或失真在所难免,如果您有更好的建议,请在博文下留言。线程安全spdlog::命名空间下的是线程安全的,当loggers在不同的线程同时执行时,下述函数不应该被调用:spdlog::set_error_handler(log_err_handler);//orlogger->set_error_handler(…

    2022年6月23日
    22
  • 服务器配置[通俗易懂]

    服务器配置[通俗易懂]Nginx配置文件的整体结构从图中可以看出主要包含以下几大部分内容:1.全局块该部分配置主要影响Nginx全局,通常包括下面几个部分:配置运行Nginx服务器用户(组)workerpro

    2022年8月4日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号