#333 Div2 Problem B Approximating a Constant Range(尺取法)
题目:http://codeforces.com/contest/602/problem/B
题意 :给出一个含有 n 个数的区间,要求找出一个最大的连续子区间使得这个子区间的最大值和最小值的差值不超过 1 ,最后输出这个子区间的长度。
分析:
因为区间里面的数只能相差1,我就用fs与fx来表示这个区间是上升区间还是下降区间
如果上升区间的话,遇到满足条件的也就是加进来区间的数与区间的开头a[st]相比较,如果是大1或者是相等就en++,直到不满足条件;
下降区间也是如此。。。
不满足条件的话st++。。同时还得重新判断区间的上升还是下降,因为经验不足,在这里卡了许久;
AC代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[];
int main()
{
int n;
scanf("%d",&n);
for(int i= ; i<n ;i++)
scanf("%d",&a[i]);
int st=,en=;
int fs=,fx=;///标记fs是表示上升,fx表示下降
int maxx=-;
while(en<=n)
{ int t=a[st];
if(a[en]-t==&&fs==)
{
fx=;
en++;
}
else if(a[en]-t==-&&fx==)
{
fs=;
en++;
}
else if(a[en]-t==)
{
en++;
}
else
{
int temp=en-st;
maxx=max(maxx,temp); st++;
int i=;
while(a[st]==a[st+i])///判断下一个区间是fs还是fx
i++;
if(a[st]-a[st+i]<)
{
fs=;
fx=;
}
else
{
fs=;
fx=;
}
}
if(en==n)
{
int temp=en-st;
maxx=max(maxx,temp);
break;
} }
printf("%d\n",maxx);
}
#333 Div2 Problem B Approximating a Constant Range(尺取法)的更多相关文章
- #333 Div2 Problem B Approximating a Constant Range (尺取 && RMQ || 尺取 && multiset)
题目链接:http://codeforces.com/contest/602/problem/B 题意 :给出一个含有 n 个数的区间,要求找出一个最大的连续子区间使得这个子区间的最大值和最小值的差值 ...
- Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分
B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- Codeforces Round #333 (Div. 2) B. Approximating a Constant Range
B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- Codeforces 602B Approximating a Constant Range(想法题)
B. Approximating a Constant Range When Xellos was doing a practice course in university, he once had ...
- FZU 2016 summer train I. Approximating a Constant Range 单调队列
题目链接: 题目 I. Approximating a Constant Range time limit per test:2 seconds memory limit per test:256 m ...
- cf602B Approximating a Constant Range
B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...
- codeforce -602B Approximating a Constant Range(暴力)
B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...
- 【32.22%】【codeforces 602B】Approximating a Constant Range
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【CodeForces 602C】H - Approximating a Constant Range(dijk)
Description through n) and m bidirectional railways. There is also an absurdly simple road network — ...
随机推荐
- java GC是在什么时候,对什么东西,做了什么事情?
1.新生代有一个Eden区和两个survivor区,首先将对象放入Eden区,如果空间不足就向其中的一个survivor区上放,如果仍然放不下就会引发一次发生在新生代的minor GC,将存活的对象放 ...
- PHP中几种加密形式
1.Md5加密和Crypt都是单向加密: 登陆时把登录密码转为md5值,然后和数据库中的进行比较. 其中crypt中的盐值支持多种: 以CRYPT_STD_DES是以/0-9A-Za-z/中的两个字符 ...
- java线程的三种实现方式
线程实现的三种种方式: 一个是继承Thread类,实现run()方法: 一个是实现Runnable接口,实现run()方法: 一个是实现Callable接口,实现call()方法:该方式和实现Runn ...
- vue mock数据设置
1.新建mock文件夹 2.添加你需要的数据例如新建商品表goods.json { "status":"0", "result":[ { & ...
- linux鸟哥的私房菜
这书还是感觉非常棒,真的是授之以渔而不是授之以鱼.我觉得只需要掌握一个命令就可以了man -k KEYWORD 比如我想查找和防火墙相关的命令,那么 man -k firewall 结果是ufw 然后 ...
- 算法Sedgewick第四版-第1章基础-009一链表与数组的比较及其他数据结构
1. 2.
- p1627 [CQOI2009]中位数
传送门 分析 https://www.luogu.org/blog/user43145/solution-p1627 代码 #include<iostream> #include<c ...
- C++面试笔记--面向对象
说到面向对象,大家第一反应应该就是它的三大特性:封装性.继承性和多态性.那么我们先简单的了解一下这三大特性: (1)封装性:封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的 ...
- Picasso VS Glide
原文: Introduction to Glide, Image Loader Library for Android, recommended by Google 在泰国举行的谷歌开发者论坛上,谷歌 ...
- [转]oracle11g 修改字符集 修改为ZHS16GBK
转至:http://www.cnblogs.com/jay-xu33/p/5210098.html sqlplus /nolog conn /as sysdba shutdown immediate; ...