HDU 5806 NanoApe Loves Sequence Ⅱ

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

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
    char c = getchar();  while(!isdigit(c)) c = getchar();
    int x = 0;
    while(isdigit(c)) { x = x * 10 + c - ‘0‘; c = getchar(); }
    return x;
}

const int maxn=200000+10;
int T,n,m,k,a[maxn],sum[maxn];

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&k); sum[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]>=m) a[i]=1;
            else a[i]=0;
            sum[i]=sum[i-1]+a[i];
        }
        LL ans=0;

        for(int i=1;i<=n;i++)
        {
            int L=i,R=n,pos=-1;
            while(L<=R)
            {
                int mid=(L+R)/2;
                if(sum[mid]-sum[i-1]>=k) pos=mid,R=mid-1;
                else L=mid+1;
            }
            if(pos==-1) continue;
            ans=ans+n-pos+1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
时间: 2024-10-13 20:59:32

HDU 5806 NanoApe Loves Sequence Ⅱ的相关文章

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 - 5806 NanoApe Loves Sequence Ⅱ 想法题

http://acm.hdu.edu.cn/showproblem.php?pid=5806 题意:给你一个n元素序列,求第k大的数大于等于m的子序列的个数. 题解:题目要求很奇怪,很多头绪但写不出,选择跳过的题,简称想法题. 首先考虑区间的更新方法:区间左端l不动,右端r滑动, 滑到有k个数>=m时,此区间符合条件,并且发现右端点再往右滑到底,此条件一直符合(因为若加入的数小于"第K大的数",则毫无影响.若不然,加入该数会产生一个新的第k大数,保证>="第K大

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 += n - j + 1 . i++ 的时候看看需不需要size-- 就可以更新了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 #define

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

题意:给出一个序列,问能找出多少个连续的子序列,使得这个子序列中第k大的数字不小于m. 分析:这个子序列中只要大于等于m的个数大于等于k个即可.那么,我们可以用尺取法写,代码不难写,但是有些小细节需要注意(见代码注释).我觉得,<挑战程序设计>里的尺取法的内容需要好好的再回顾一下= =. 代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 using namesp

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

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

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

先找相邻差值的最大,第二大,第三大 删去端点会减少一个值, 删去其余点会减少两个值,新增一个值,所以新增和现存的最大的值比较一下取最大即可 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 #define LL long long 6 const int N = 100005; 7 int t, n, p1, p2, p3; 8 LL a[N]; 9

HDU 5805 NanoApe Loves Sequence

处理出每个位置左边的最大值和右边的最大值.然后就可以o(1)计算去掉某位置的最大值了. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #includ

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

题目:传送门. 题意:题目说的是求期望,其实翻译过来意思就是:一个长度为 n 的数列(n>=3),按顺序删除其中每一个数,每次删除都是建立在最原始数列的基础上进行的,算出每次操作后得到的新数列的相邻两数的差的绝对值的最大值,求这些n个最大值的总和. 题解:把n=3的情况单独拿出来直接算出来,就是abs(data[3]-data[2])+abs(data[2]-data[1])+abs(data[3]-data[1]),然后讨论n>=4的情况.首先遍历求出原始数列的相邻两数的差的绝对值的最大值m