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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • eclipse经常卡死的解决方法总结_eclipse运行一段时间后死机

    eclipse经常卡死的解决方法总结_eclipse运行一段时间后死机使用eclipse3.6版本时,每当用alt+/或.来自动补全代码时,eclipse经常会卡死。这是eclipse3.6版本的一个bug,网上有朋友直接选择打补丁,也有人配置eclipse的Contentassist延时加长来解决这个问题。既然是版本问题,还是直接治标好了,升级新版本的eclipse解决这个问题:Help->checkforupdates选择要更新的组件

    2022年10月10日
  • 算法刷题LeetCode中文版_leetcode100题

    算法刷题LeetCode中文版_leetcode100题算法题打卡:仅仅反转字母。没有特别幸运,那么请先特别努力,别因为懒惰而失败,还矫情地将原因归于自己倒霉。所以说,树倒了,没有一片雪花是无辜的

    2022年8月31日
  • 递归下降算法_递归下降分析程序得到的经验

    递归下降算法_递归下降分析程序得到的经验递归下降算法算法模型:Term=Term+ExprExpr=Expr+FactorFactor=单个元素。最小单位。 实现原理:一个程式进入算法及被看作是一个项,分解成项加表达式的形式,表达式被分解成表达式加因子的形式,因子是这个算法中的最小单位。上一级调用比自己小一级的自己。这里三层分离,越下层模型中所形成的优先级就会越高。 我用递归下降算法写了个简单的计算器,递归算法为我的运算符号…

    2022年4月20日
  • linux 卸载cuda_如何卸载CUDA

    linux 卸载cuda_如何卸载CUDALinux下cuda卸载亲测好用#1.用runfile方式安装的删除方法:sudo/usr/local/cuda-8.0/bin/uninstall_cuda_8.0.pl#2.用packagemanager方式安装的删除方法:sudoapt-get–purgeremovecuda-8.0sudoaptautoremovesudoapt-getautoclean#cudnn文件和samples残留在/usr/local/cuda-8.0/,删除

    2022年9月6日
  • quartus ii15.1安装教程_quartus ii9.1安装教程

    quartus ii15.1安装教程_quartus ii9.1安装教程安装步骤:安装前先关闭杀毒软件和360卫士,注意安装路径不能有中文,存放安装包的路径最好也不要有中文。1.解压安装包。2.安装前下载相应的器件库文件。再安装包–>器件库下载地址–>器件库下载地址.txt。复制器件库中需要的器件的下载地址。3.下载好库文件后将它放在软件安装程序同一路径。运行安装程序。4.点击NEXT。5.点击Iaccept,然后点击NEXT。…

    2022年10月15日
  • LSD_SLAM编译之一气呵成法

    LSD_SLAM编译之一气呵成法LSD_SLAM编译之平台信息本LSD_SLAM编译平台信息:ubuntu16.04LSopencv3.XROS—kinetic其他的都不重要…ROS_kinetic的安装参考点击此处准备及安装注意:一定要下载此处的LSD_SLAM官方的lsd_slam一直没有编译成功,此LSD_SLAM已经被该作者fixedbugs.所以我们直接下载该git。…

    2022年5月7日

发表回复

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

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