#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 — ...
随机推荐
- matlab图片高清复制到visio
编辑→复制图窗→在visio中粘贴
- Ajax笔记(二)
JSON基本概念: JSON:javaScript对象表示法(JavaScript Object Notation) JSON是存储和交换文本信息的语法,类似XML.它采用键值对的方式来组织,易于人们 ...
- gitlab 添加ssh秘钥
在创建新的ssh秘钥对之前,要先确认一下电脑中是否已经有了一对秘钥: Git Bash on Windows / GNU/Linux / macOS / PowerShell: cat ~/.ssh/ ...
- resize和reserve的区别
转自http://blog.csdn.net/jackywgw/article/details/6248342 首先必须弄清楚两个概念: 1.capacity 指容器在分配新的存储空间之前能存储的元素 ...
- 除了ROS ,机器人自主定位导航还能怎么做?
博客转载自:https://www.leiphone.com/news/201609/10QD7yp7JFV9H9Ni.html 雷锋网(公众号:雷锋网)按:本文作者科技剪刀手,思岚科技技术顾问. 随 ...
- php学习笔记-elseif
<?php if(condition1) { func1(); }elseif(condition2) { func2(); }else { func3(); } ?> elseif需要明 ...
- Mac OS X 安装教程
1)10.8.3 http://bbs.pcbeta.com/viewthread-1317754-1-1.html 2)10.9 http://blog.csdn.net/kissing_huo/a ...
- 999F Cards and Joy
传送门 题目大意 有n个人n*m张牌,每个人分m张牌.每个人有一个自己喜欢的数值,如果他的牌中有x张数值等于这个值则他的高兴度为L[x],求怎样分配牌可以使得所有人的总高兴度最大. 分析 我们发现每一 ...
- CF 432B :Football Kit
hash做法: #include<stdio.h> #include<string.h> ; int home[Max],away[Max],hash[Max]; int ma ...
- React 和 Redux理解
学习React有一段时间了,但对于Redux却不是那么理解.网上看了一些文章,现在把对Redux的理解总结如下 从需求出发,看看使用React需要什么 1. React有props和state pro ...