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


相关推荐

  • Parallel.ForEach 使用多线程遍历循环

    Parallel.ForEach 使用多线程遍历循环原地址路径:https://www.cnblogs.com/personblog/archive/2019/10/09/11640801.htmlParallel.ForEach相对于foreach是多线程,并行操作;foreach是单线程循环操作。staticvoidMain(string[]args){Console.WriteLine(“HelloWorld!”);List<UserInfo>lst=n

    2022年7月19日
    13
  • mysql的case when语法_sql基本语句大全

    mysql的case when语法_sql基本语句大全介绍mysql数据库中casewhen语句的用法,首先介绍casewhen语句的基础知识,然后提供了相关例子。(1)mysql数据库中CASEWHEN语句。casewhen语句,用于计算条件列表并返回多个可能结果表达式之一。CASE具有两种格式:简单CASE函数将某个表达式与一组简单表达式进行比较以确定结果。CASE搜索函数计算一组布尔表达式以确定结果。两种格式都支持…

    2022年9月5日
    3
  • 西班牙语语法【1:名词】

    西班牙语语法【1:名词】阴阳性规则

    2022年6月6日
    36
  • python构造方法的作用_python中类的作用

    python构造方法的作用_python中类的作用1.构造函数的作用构造函数主要用来在创建对象时完成对对象属性的一些初始化等操作,当创建对象时,对象会自动调用它的构造函数。一般来说,构造函数有以下三个方面的作用:■给创建的对象建立一个标识符;■为对象数据成员开辟内存空间;■完成对象数据成员的初始化。2.默认构造函数当用户没有显式的去定义构造函数时,编译器会为类生成一个默认的构造函数,称为”默认构造函数”,默认构造函数不能完成…

    2022年9月8日
    0
  • Cocos creator 微信小游戏排行榜「建议收藏」

    Cocos creator 微信小游戏排行榜「建议收藏」Cocoscreator版本:2.0.10主域动态刷新子域首先得知道主域和子域的关系,需要让子域(开放域)里的排行榜信息动态刷新在主域的页面中,需要在主域中的节点上挂载WXSubContextView组件。子域中,可以添加Widget组件、滚动的节点来实现上下或者左右滑动。这里主要记录排行榜的实现。(这里只实现了同玩好友排行榜的功能,同玩群好友可以另实现)实现排行榜步骤一:需要实现…

    2025年7月12日
    0
  • 整型转字符串c语言_java字符串数组转字符串

    整型转字符串c语言_java字符串数组转字符串整型转字符串实现(C语言)second6020180529#include&lt;stdio.h&gt;//n&lt;2的32次数,所以最大10位//n可能为负数,也可能为正数voidint2str(intn,char*str){charbuf[10]="";inti=0;intlen=0;//temp为n的绝…

    2022年10月19日
    0

发表回复

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

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