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


相关推荐

  • 从几何角度理解反函数的导数[通俗易懂]

    从几何角度理解反函数的导数[通俗易懂]从几何角度理解反函数的导数在同一个函数图像中,反函数和函数表达式是对同一个函数的不同表示tan⁡(π2−α)=tan⁡β cot⁡α=tan⁡β 1tan⁡α=tan⁡β 1f′(x)=φ′(y)\tan(\frac{\pi}{2}-\alpha)=\tan\beta\\~\\\cot\alpha=\tan\beta\\~\\\frac{1}{\tan\alpha}=\tan\beta\\~\\\frac{1}{f'(x)}=\varphi'(y)tan

    2025年6月28日
    4
  • 【Python】 【绘图】plt.figure()的使用

    【Python】 【绘图】plt.figure()的使用1.figure语法及操作(1)figure语法说明figure(num=None,figsize=None,dpi=None,facecolor=None,edgecolor=None,frameon=True)num:图像编号或名称,数字为编号,字符串为名称 figsize:指定figure的宽和高,单位为英寸; dpi参数指定绘图对象的分辨率,即每英寸多少个像素,…

    2022年6月29日
    50
  • 算术几何平均matlab,算术-几何平均数——高斯的发现

    算术几何平均matlab,算术-几何平均数——高斯的发现“算术-几何平均数”既不是算术平均数,也不是几何平均数,由素有“数学王子”之称的德国数学家高斯首先发现和研究。算术-几何平均数,当然与“算术平均数”和“几何平均数”这两个概念有很深的关系。我们知道,但凡一个数学概念或定理,哪怕再简单不过,只要和高斯扯上关系,那就一定不简单了。带着耐心,我们来看看高斯关于算术-几何平均数的研究。预备知识对于两个正实数a和b(不妨设0我们有基本不等式,等号当且仅当a=…

    2025年6月7日
    4
  • TimerTask(addin timer语音)

    其实就Timer来讲就是一个调度器,而TimerTask呢只是一个实现了run方法的一个类,而具体的TimerTask需要由你自己来实现,例如这样:Timertimer=newTimer();timer.schedule(newTimerTask(){ publicvoidrun(){ System.out.println(“abc”); }},200

    2022年4月18日
    41
  • STM32F103 AFIO时钟疑问

    STM32F103 AFIO时钟疑问

    2022年3月3日
    49
  • IDEA激活成功教程后一直提示JetbrainsAgent 相关的弹框问题

    IDEA激活成功教程后一直提示JetbrainsAgent 相关的弹框问题激活成功教程后打开IDEA就弹框,关闭之后会自动打开浏览器,隔一会也会弹出来 也是一样的问题一开始是说把txt 和 jar 文件放一个路径下之类的方法,几经波折,发现没任何用处~最后各种搜索排查,在设置下更改配置就不弹啦~settings设置下搜索agent 取消”Instrumenting agent(requires debugger restart)”在 Reload classes after compilation:选择第一个 Always…

    2022年8月19日
    8

发表回复

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

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