ACdream 1427—— Nice Sequence——————【线段树单点更新,区间查询】
Nice Sequence
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
Output
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——————【线段树单点更新,区间查询】的更多相关文章
- HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)
HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...
- NYOJ-568/1012//UVA-12299RMQ with Shifts,线段树单点更新+区间查询
RMQ with Shifts 时间限制:1000 ms | 内存限制:65535 KB 难度:3 -> Link1 <- -> Link2 <- 以上两题题意是一样 ...
- hihoCoder week19 RMQ问题再临-线段树 单点更新 区间查询
单点更新 区间查询 #include <bits/stdc++.h> using namespace std; #define m ((l+r)/2) #define ls (rt< ...
- HDU 1166敌兵布阵+NOJv2 1025: Hkhv love spent money(线段树单点更新区间查询)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- HDU1166(线段树单点更新区间查询)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- CDOJ 1073 线段树 单点更新+区间查询 水题
H - 秋实大哥与线段树 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit S ...
- HDU 1754.I Hate It-结构体版线段树(单点更新+区间查询最值)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- Who Gets the Most Candies? POJ - 2886(线段树单点更新+区间查询+反素数)
预备知识:反素数解析 思路:有了反素数的解法之后就是线段树的事了. 我们可以用线段树来维护哪些人被淘汰,哪些人没被淘汰,被淘汰的人的位置,没被淘汰的人的位置. 我们可以把所有人表示为一个[1,n]的区 ...
- NBUT 1602 Mod Three(线段树单点更新区间查询)
[1602] Mod Three 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Please help me to solve this problem, if so, Liang ...
- HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模
Multiply game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
随机推荐
- Qt乱码解决办法(常量中有换行符)
用记事本打开源代码,然后点另存为,utf-8,编码覆盖 QStringLiteral("打开相机")
- 【总结整理】webstorm插件使用
<ul> <li><a href="#">1F 男装</a></li> <li><a href=&qu ...
- 利用Admin-LTE项目搭建自己前端的开发框架模板
1 完整版本 1.1 下载admin-lte项目源代码 github下载地址:点击前往 三少云盘地址:点击前往 1.2 查看admin-lte精简主页源代码 页面地址:点击前往 注意:由源代码可以知道 ...
- 八数码 Java实现
参考http://blog.csdn.net/helloworld10086/article/details/41853389 package com.EightNumber.view; import ...
- JavaScript高级程序设计学习笔记--面向对象的程序设计(二)-- 继承
相关文章: 面向对象的程序设计(一) — 创建对象 http://www.cnblogs.com/blackwood/archive/2013/04/24/3039523.html 继承 继承是OO语 ...
- Netty 线程模型与Reactor 模式
前言 Netty 的线程模型是基于NIO的Selector 构建的,使用了异步驱动的Reactor 模式来构建的线程模型,可以很好的支持成百上千的 SocketChannel 连接.由于 READ/W ...
- 更改数据,ExecuteNonQuery()
using (mycon) { mycon.Open(); string MyTime; DateTime dtDate; MyTime = textBox1.Text.ToString(); str ...
- ApiDoc 一键生成注释
本文来自网易云社区. 作者:盛国存 背景 我们日常在使用ApiDoc维护管理api文档,提高了api文档的整体维护性.但在老旧接口中,补充接口注解无疑是一次繁重的体力劳动.仔细查看,大多数接口的格式 ...
- git 把文件从 版本管理中移除 andorid版本
刚学git时,一股脑吧所有文件全部加到版本管理中,现在做Android开发,这样做就有很大的问题了,gen 和bin 文件夹下的文件是编译生成的,最好不要加到版本管理中,最好加入到.gitigno ...
- 通过jdbc连接MySql数据库的增删改查操作
一.获取数据库连接 要对MySql数据库内的数据进行增删改查等操作,首先要获取数据库连接 JDBC:Java中连接数据库方式 具体操作如下: 获取数据库连接的步骤: 1.先定义好四个参数 String ...