Nice Sequence

Time Limit: 4000/2000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others)
Submit Status

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 a1, a2,..., al 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 题目大意:给出n,k。n表示长度为n的序列,且序列中的值都是0---n的。定义ci,j表示数字i在a1,a2...aj中出现的次数,定义k-nice序列,即i1<i2且满足不等式ci1,j ≥ ci2,j −k。 解题思路:我们将序列映射到线段树的叶子上。那么对于ai来说,我们单点更新数字ai出现的次数,然后从1---ai中查询某数字出现最少的次数,如果不满足的不等式,那么就直接结束了。其实只需要用线段树维护每个数字出现的次数即可。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int INF=0x3f3f3f3f;
const int maxn=3*(1e5);
struct SegTree{
int v;
}segtrees[maxn*4];
int a[maxn],times[maxn];
void build(int rt,int L,int R){
if(L==R){
segtrees[rt].v=0;
return ;
}
build(lson);
build(rson);
}
void PushUp(int rt){
segtrees[rt].v=min(segtrees[rt*2].v,segtrees[rt*2+1].v);
}
void update(int rt,int L,int R,int key){
if(L==R){
segtrees[rt].v++;
return ;
}
if(key<=mid)
update(lson,key);
else
update(rson,key);
PushUp(rt);
}
int query(int rt,int L,int R,int l_ran,int r_ran){
if(l_ran<=L&&R<=r_ran){
return segtrees[rt].v;
}
int ret=INF;
if(l_ran<=mid){
ret =min(ret,query(lson,l_ran,r_ran));
}
if(r_ran>mid){
ret = min(ret,query(rson,l_ran,r_ran));
}
return ret;
}
int main(){
int n,k;
while(scanf("%d%d",&n,&k)!=EOF){
memset(times,0,sizeof(times));
build(1,0,n);
int flag=0;
int ans=n;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(flag) continue;
times[a[i]]++;
update(1,0,n,a[i]);
int xx=query(1,0,n,0,a[i]);
if(xx<times[a[i]]-k){
ans=i-1;
flag=1;
}
}
printf("%d\n",ans);
}
return 0;
}

  

ACdream 1427—— Nice Sequence——————【线段树单点更新,区间查询】的更多相关文章

  1. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

  2. NYOJ-568/1012//UVA-12299RMQ with Shifts,线段树单点更新+区间查询

    RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 ->  Link1  <- -> Link2  <- 以上两题题意是一样 ...

  3. hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询

    单点更新 区间查询 #include <bits/stdc++.h> using namespace std; #define m ((l+r)/2) #define ls (rt< ...

  4. HDU 1166敌兵布阵+NOJv2 1025: Hkhv love spent money(线段树单点更新区间查询)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  5. HDU1166(线段树单点更新区间查询)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  6. CDOJ 1073 线段树 单点更新+区间查询 水题

    H - 秋实大哥与线段树 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Submit S ...

  7. HDU 1754.I Hate It-结构体版线段树(单点更新+区间查询最值)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  8. Who Gets the Most Candies? POJ - 2886(线段树单点更新+区间查询+反素数)

    预备知识:反素数解析 思路:有了反素数的解法之后就是线段树的事了. 我们可以用线段树来维护哪些人被淘汰,哪些人没被淘汰,被淘汰的人的位置,没被淘汰的人的位置. 我们可以把所有人表示为一个[1,n]的区 ...

  9. NBUT 1602 Mod Three(线段树单点更新区间查询)

    [1602] Mod Three 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Please help me to solve this problem, if so, Liang ...

  10. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

随机推荐

  1. 使用SVG + CSS实现动态霓虹灯文字效果

    效果图: 原理:多个SVG描边动画使用不同的animation-delay即可! 对于一个形状SVG元素或文本SVG元素,可以使用stroke-dasharray来控制描边的间隔样式,并且可以用str ...

  2. C# 自定义颜色

    一.需要引用 using System.Windows.Media; 二. 自定义颜色 通过自定义 RGB 的值来达到自定义颜色的目的 Color _Mycolor = Color.FromRgb(5 ...

  3. LaTeX技巧203:如何实现等号对齐_LaTeX_Fun_新浪博客

    LaTeX技巧203:如何实现等号对齐_LaTeX_Fun_新浪博客 我们在进行公式的输入排版的时候,通常希望公式比较齐整,所以需要一些等号对齐,或者左对齐,关于公式的左对齐前文已经介绍了方法.htt ...

  4. #ifdef-#endif的作用及其使用技巧

    电脑程序语句,我们可以用它区隔一些与特定头文件.程序库和其他文件版本有关的代码. 1 2 3 #ifdef语句1 //程序2 #endif 可翻译为:如果宏定义了语句1则程序2. 作用:我们可以用它区 ...

  5. Postman(调试工具)

    Postman Postman用法简介-Http请求模拟工具 时间 2015-09-26 23:52:00  博客园-原创精华区 原文  http://www.cnblogs.com/codingbl ...

  6. 安装yum在ubnutu上

    安装yum在ubnutu上1:首先检测是否安装了build-essential程序包 apt-get install build-essential2.安装yumapt-get install yum ...

  7. Ubuntu12.04安装R ,Rstudio, RHive

    环境: Ubuntu12.04 R-3.1.0 0.Ubuntu安装R官网的介绍 http://mirrors.ustc.edu.cn/CRAN/ Precise Pangolin (12.04; L ...

  8. CSS学习系列4 -- 再说CSS中的浮动运用及clear:left/right实际用法

    在 CSS学习系列2 -- CSS中的清除浮动 中,我们详细说了CSS中清除浮动的方法及使用 后来我自己在项目开发一个需要使用浮动的网页时,进行了实际运用,加上后来看到一篇好文章.所以就在这里再次写篇 ...

  9. C#的递归函数

    我的理解:函数内部把自己当另一个函数来调用 其实还是调用的他自己 递归函数必须有结束条件,否则就是死循环

  10. 「学习笔记」Fast Fourier Transform

    前言 快速傅里叶变换(\(\text{Fast Fourier Transform,FFT}\) )是一种能在\(O(n \log n)\)的时间内完成多项式乘法的算法,在\(OI\)中的应用很多,是 ...