BC#86 1003NanoApe Loves Sequence Ⅱ[two-pointer]

NanoApe Loves Sequence Ⅱ

Accepts: 374

Submissions: 946

Time Limit: 4000/2000 MS (Java/Others)

Memory Limit: 262144/131072 K (Java/Others)

问题描述

退役狗 NanoApe 滚回去学文化课啦!

在数学课上,NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为 nn 的数列,他又根据心情写下了一个数 mm。

他想知道这个数列中有多少个区间里的第 kk 大的数不小于 mm,当然首先这个区间必须至少要有 kk 个数啦。

输入描述

第一行为一个正整数 TT,表示数据组数。

每组数据的第一行为三个整数 nmkn,m,k。

第二行为 nn 个整数 AiA?i??,表示这个数列。

1T10 2n200000 1kn/2 1mAi1091≤T≤10, 2≤n≤200000, 1≤k≤n/2, 1≤m,A?i??≤10?9??

输出描述

对于每组数据输出一行一个数表示答案。

输入样例

1
7 4 2
4 2 7 7 6 5 1

输出样例

18


才知道原来有two-pointer这个名字

>=m的数为1,其他为0,那么就是求有多少个区间和>=k

滑动窗口,i是头,p是尾

//
//  main.cpp
//  bc86-1003
//
//  Created by Candy on 10/1/16.
//  Copyright © 2016 Candy. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
using namespace std;
const int N=2e5+5;
inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
    return x;
}
int T,n,m,k,a[N];
int main(int argc, const char * argv[]) {
    T=read();
    while(T--){
        n=read();m=read();k=read();
        for(int i=1;i<=n;i++){a[i]=read();a[i]=a[i]>=m?1:0;}
        int cnt=0,p=1;
        long long ans=0;
        for(int i=1;i<=n-k+1;i++){
            while(cnt<k&&p<=n)
                cnt+=a[p++];
            if(cnt>=k) ans+=(long long)(n-p+1+1);//p already add 1
            cnt-=a[i];
        }
        printf("%lld\n",ans);
    }

    return 0;
}
时间: 2024-12-26 13:53:35

BC#86 1003NanoApe Loves Sequence Ⅱ[two-pointer]的相关文章

Best Coder #86 1002 NanoApe Loves Sequence

NanoApe Loves Sequence Accepts: 531 Submissions: 2481 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others) Problem Description NanoApe, the Retired Dog, has returned back to prepare for the National Higher Education Entr

【BC#24 1002 HDOJ 5273】Dylans loves sequence

[BC#24 1002 HDOJ 5273]Dylans loves sequence 逆序对动归 比赛时候各种奇葩姿势都上了个遍 归并也憋出来了 谁知道就给我看这个.... 赛后有的思路 收到赛后题解的启发 dp[l][r]是l~r之间逆序对 先暴力把dp[1][1~n]枚举出来 然后i从2~n枚举左边界 右边界从i+1~n 这样dp[i][j] 即求出了区间左端点从2往后的所有逆序对数 而dp[i][j]即为dp[i-1][j]中吧i-1的逆序对数减去的余数 这样顺序暴力即可......还是

HDU 5806 NanoApe Loves Sequence Ⅱ(尺取+思维)——BestCoder Round #86 1003

传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 514    Accepted Submission(s): 248 Problem Description NanoApe, the Retired Dog, has returned back to prepare for f

hdu 5273 Dylans loves sequence 逆序数简单递推

Dylans loves sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5273 Description Dylans得到了N个数a[1]...a[N].有Q个问题,每个问题形如(L,R)他需要求出L−R这些数中的逆序对个数.更加正式地,他需要求出二元组(x,y)的个数,使得L≤x,y≤R且x<y且a[x]>a[y] Input 第一行有两个数N和Q

HDU 5805 NanoApe Loves Sequence(思维)

传送门 NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 440    Accepted Submission(s): 205 Problem Description NanoApe, the Retired Dog, has returned back to prepare for the

hdu_5806_NanoApe Loves Sequence Ⅱ(双指针)

题目链接:hdu_5806_NanoApe Loves Sequence Ⅱ 题意: 给你一段数,问你有多少个区间满足第K大的数不小于m 题解: 直接双指针加一下区间就行 1 #include<cstdio> 2 #include<algorithm> 3 #define F(i,a,b) for(int i=a;i<=b;i++) 4 using namespace std; 5 typedef long long ll; 6 const int N=2e5+7; 7 in

5806 NanoApe Loves Sequence Ⅱ(尺取法)

传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 1585    Accepted Submission(s): 688 Description NanoApe, the Retired Dog, has returned back to prepare for for the

5805 NanoApe Loves Sequence

传送门 NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others) Total Submission(s): 1323    Accepted Submission(s): 521 Description NanoApe, the Retired Dog, has returned back to prepare for the Natio

NanoApe Loves Sequence Ⅱ(尺取法)

题目链接:NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 339    Accepted Submission(s): 165 Problem Description NanoApe, the Retired Dog, has returned back to prepare for