当前位置: 代码迷 >> 综合 >> HDOJ 5806 NanoApe Loves Sequence Ⅱ
  详细解决方案

HDOJ 5806 NanoApe Loves Sequence Ⅱ

热度:55   发布时间:2023-12-06 03:13:57.0

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5806


作为HDU集训队的一员,我很清楚Claris是一位数据结构大师,但正是如此,比赛的时候小编老是往数据结构上去想,哎,然而此题完全不用(=.= GG),只需要用到双指针就能够解决。

仔细思考,我们是要区间里面第k大的数大于m,那么其实只要区间里面有k个大于m的数就可以了,接下来就只需要用到双指针就行了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 200000+5;
typedef long long LL;
int a[maxn];
int main()
{int T;scanf("%d", &T);while(T--){int n,m,k;scanf("%d%d%d", &n, &m, &k);for(int i=0; i<n; i++){int tmp;scanf("%d", &tmp);a[i] = (tmp >= m);}LL L,R,sum,ans;L = R = sum = ans = 0;sum = a[0];while(R < n){if(sum == k){ans += n-R;sum -= a[L];L++;}else{R++;sum += a[R];}}printf("%I64d\n", ans);}
}


  相关解决方案