题意:给出一个序列,问能找出多少个连续的子序列,使得这个子序列中第k大的数字不小于m。

  分析:这个子序列中只要大于等于m的个数大于等于k个即可。那么,我们可以用尺取法写,代码不难写,但是有些小细节需要注意(见代码注释)。我觉得,《挑战程序设计》里的尺取法的内容需要好好的再回顾一下= =。

  代码如下:

 #include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long ll;
const int N = + ; int a[N]; int main()
{
int T;scanf("%d",&T);
while(T--)
{
int n,m,k;scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++) scanf("%d",a+i);
int s = , t = , num = ;
ll ans = ;
for(;;)
{
while(num<k && t<=n)
{
if(a[t++] >= m)
{
num++;
}
}
//if(t>n) break;
if(num < k) break;
// 必须采取下面的写法,不然会WA
// 因为可能即使 t>n 出来此时刚好也 num==k 了,必须再加一次。
// 也可以为了保险,在下面这个语句前面加上 if(num >= k)
ans += n-t+;
if(a[s++] >= m) num--;
}
printf("%I64d\n",ans);
}
}

———————————————————2017.1.18分界线———————————————————————

  又做了一遍这题,上面的说法是错误的,循环退出的条件应该是起点s>n而不是终点t>n。而上面num<k的写法正确是因为,num<k此时t肯定>n才退出里面的循环,而终点不变,起点一直前进说明范围一直减少,num只会更少,因此再也不可能出现num>=k的情况,所以可以直接退出了。但是直接判断s来判断循环是否结束是肯定没有错的(虽然时间上可能会差一点,但还是O(n)的)。

  重写的代码如下:

 #include <bits/stdc++.h>
using namespace std;
const int N = + ;
typedef long long ll; int a[N];
int T,n,k,m; int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++) scanf("%d",a+i);
int s = , t = ;
int num = ;
ll ans = ;
while()
{
while(num < k && t <= n)
{
if(a[t++] >= m) num++;
}
if(num >= k) ans += n - (t-) + ;
if(a[s++] >= m) num--;
if(s == n + ) break;
}
printf("%I64d\n",ans);
}
return ;
}

HDU 5806 NanoApe Loves Sequence Ⅱ ——(尺取法)的更多相关文章

  1. Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) (C++,Java)

    Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) Hdu 5806 题意:给出一个数组,求区间第k大的数大于等于m的区间个数 #include<queue> # ...

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

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

  3. NanoApe Loves Sequence Ⅱ(尺取法)

    题目链接:NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 ...

  4. hdu-5806 NanoApe Loves Sequence Ⅱ(尺取法)

    题目链接: NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 262144/13107 ...

  5. HDU5806:NanoApe Loves Sequence Ⅱ(尺取法)

    题目链接:HDU5806 题意:找出有多少个区间中第k大数不小于m. 分析:用尺取法可以搞定,CF以前有一道类似的题目. #include<cstdio> using namespace ...

  6. HDU 5806 NanoApe Loves Sequence Ⅱ

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

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

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

  8. 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 + ...

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

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

随机推荐

  1. vs 2017创建类时的默认模板修改

    思路:找到vs 2017安装目录---->找到模板文件---->修改 一般安装目录: C:\Program Files (x86)\Microsoft Visual Studio\2017 ...

  2. O033、Terminate Instance 操作详解

    参考https://www.cnblogs.com/CloudMan6/p/5486066.html   本节通过日志详细分析 Nova Terminate 操作.Terminate 操作就是删除 i ...

  3. 04 Go语言之包

    1.为什么有包这个概念? 1)开发中,往往要在不同的文件中调用其他文件的函数 2)Go代码最小粒度单位是”包” 3)go的每一个文件都属于一个包,通过package管理 4)go以包的形式管理文件和项 ...

  4. 微信小程序常用事件

    bind bind事件绑定不会阻止冒泡事件向上冒泡,catch事件绑定可以阻止冒泡事件向上冒泡. bindtap  跳转页面 bindchange  .value 改变时触发 change 事件 bi ...

  5. mysql 触发器 if then elseif else 的运用

    create procedure dbname.proc_getGrade (stu_no varchar(20),cour_no varchar(10)) BEGIN declare stu_gra ...

  6. go语言时间函数

    以YY-mm-dd HH:MM:SS.9位 输出当前时间: func main() { fmt.Println(time.Now()) // 2019-11-15 16:26:12.4807588 + ...

  7. vue中 localStorage的使用方法(详解)

    vue中实现本地储存的方法:localStorage,在HTML5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题(cooki ...

  8. RHEL7 网口绑定Network Teaming

    1.选择Networking Teaming配置方法 使用文本用户界面工具nmtui 使用命令行工具nmcli 使用ifcfg配置文件创建网络成组 使用图形用户界面配置网络成组     2.了解主接口 ...

  9. 4.4. Item Pipeline管道文件

    0:Spider爬取数据 # -*- coding: utf-8 -*- import scrapy import sys from mySpider.items import ItcastItem ...

  10. 【vue lazyload】插件的使用步骤

    VUE图片懒加载-vue lazyload插件的简单使用