Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针)

Hdu 5806

题意:给出一个数组,求区间第k大的数大于等于m的区间个数

#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const int N=2e5+10;
int a[N];
int main()
{
int T=read();
while(T--){
int n=read(),m=read(),k=read();
for(int i=0;i<n;i++) a[i]=read();
int l=0,r=0,cnt=0;
ll ans=0;
while(l<=r&&r<n){
while(r<n&&cnt<k){
if(a[r++]>=m) cnt++;
}
if(r>n||cnt<k) break;
if(cnt==k) ans+=n-r+1;
while(cnt==k){
if(a[l++]>=m) cnt--;
if(cnt==k) ans+=n-r+1;
}
}
printf("%lld\n",ans);
}
return 0;
}

Java版本实现,输入可能会导致超时,需要使用BufferedInputStream加速

import java.io.*;
import java.util.*;
class MyInputStream extends InputStream {
public BufferedInputStream bis = new BufferedInputStream(System.in);
public int read() throws IOException {
int i;
while ((i = bis.read()) < 48)
if (i == -1)
return -1;
int temp = 0;
while (i > 47) {
temp = temp * 10 + i - 48;
i = bis.read();
}
return temp;
}
}
public class Main
{
static final int N=200005;
static int a[]=new int[N];
public static void main(String[] args) throws IOException{
MyInputStream sc=new MyInputStream();
int T=sc.read();
Integer zero=new Integer("0");
while(!zero.equals(T--)){
int n=sc.read(),m=sc.read(),k=sc.read();
for(int i=0;i<n;i++) a[i]=sc.read();
int l=0,r=0,cnt=0;
long ans=0;
while(l<=r&&r<n){
while(r<n&&cnt<k){
if(a[r++]>=m) cnt++;
}
if(r>n||cnt<k) break;
if(cnt==k) ans+=n-r+1;
while(cnt==k){
if(a[l++]>=m) cnt--;
if(cnt==k) ans+=n-r+1;
}
}
System.out.println(ans);
}
sc.close();
}
}

Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) (C++,Java)的更多相关文章

  1. HDU 5806 NanoApe Loves Sequence Ⅱ (模拟)

    NanoApe Loves Sequence Ⅱ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5806 Description NanoApe, t ...

  2. HDU - 5806 NanoApe Loves Sequence Ⅱ 想法题

    http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出, ...

  3. HDU 5806 - NanoApe Loves Sequence Ⅱ (BestCoder Round #86)

    若 [i, j] 满足, 则 [i, j+1], [i, j+2]...[i,n]均满足 故设当前区间里个数为size, 对于每个 i ,找到刚满足 size == k 的 [i, j], ans + ...

  4. HDU 5806 NanoApe Loves Sequence Ⅱ

    将大于等于m的数改为1,其余的改为0.问题转变成了有多少个区间的区间和>=k.可以枚举起点,二分第一个终点 或者尺取法. #pragma comment(linker, "/STACK ...

  5. HDU 5806 NanoApe Loves Sequence Ⅱ ——(尺取法)

    题意:给出一个序列,问能找出多少个连续的子序列,使得这个子序列中第k大的数字不小于m. 分析:这个子序列中只要大于等于m的个数大于等于k个即可.那么,我们可以用尺取法写,代码不难写,但是有些小细节需要 ...

  6. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

  7. HDU 5805 NanoApe Loves Sequence (模拟)

    NanoApe Loves Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5805 Description NanoApe, the ...

  8. HDU 5805 NanoApe Loves Sequence (思维题) BestCoder Round #86 1002

    题目:传送门. 题意:题目说的是求期望,其实翻译过来意思就是:一个长度为 n 的数列(n>=3),按顺序删除其中每一个数,每次删除都是建立在最原始数列的基础上进行的,算出每次操作后得到的新数列的 ...

  9. HDU 5805 - NanoApe Loves Sequence (BestCoder Round #86)

    先找相邻差值的最大,第二大,第三大 删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可 #include <iostream> #inc ...

随机推荐

  1. oracle 定时器调用存储过程

    转载请说明出处:http://t22011787.iteye.com/blog/1112745 一.查询系统中的job,可以查询视图 --相关视图 select * from dba_jobs; se ...

  2. bzoj 2200: [Usaco2011 Jan]道路和航线【spfa】

    直接跑最短路就行了--还不用判负环 #include<iostream> #include<cstdio> #include<queue> using namesp ...

  3. Django day 34 过滤课程,登录,redis,python操作redis

    一:过滤课程, 二:登录 三:redis, 四:python操作redis

  4. Snackbar:用它来替换Toast 显示短提示

    简介 Snackbar 它是Toast的子类.主要用来提示短暂的提示信息后,然后它自动消失. 它寄生在普通view上,具有一些基本功能. 它寄生在 CoordinatorLayout 时,有以下两个特 ...

  5. Spring Cloud学习(一)

    SpringCloud是什么? Spring Cloud是一个微服务框架,相比Dubbo等RPC框架, Spring Cloud提供的全套的分布式系统解决方案. Spring Cloud对微服务基础框 ...

  6. C# 访问mongodb数据库

    1.引用四个mongodb动态库MongoDB.Bson.dll,MongoDB.Driver.Core.dll,MongoDB.Driver.dll,MongoDB.Driver.Legacy.dl ...

  7. LN : leetcode 515 Find Largest Value in Each Tree Row

    lc 515 Find Largest Value in Each Tree Row 515 Find Largest Value in Each Tree Row You need to find ...

  8. 可以在一个html的文件当中读取另一个html文件的内容

    1.IFrame引入,看看下面的代码 <IFRAME NAME="content_frame" width=100% height=30 marginwidth=0 marg ...

  9. Android APK瘦身之webp图片

    webp格式是谷歌推出的一种有损压缩格式,这种图片格式相比png或者jpg格式的图片损失的质量几乎可以忽略不计,但是压缩后图片的体积却比png或者jpg要小很多.亲测一个100kb的png图片经过we ...

  10. 3星|《OKR:源于英特热和谷歌的目标管理利器》:OKR原理、实施手册、实施过的公司的访谈

    OKR原理与实施手册,另外附了几家实施过OKR的公司的访谈. 书中表述的OKR思想,我认为是这两点: 1:始终聚焦在最重要的目标上: 2:不以OKR为考核员工的指标: Kindle电子版排版有小缺陷, ...