#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 — ...
随机推荐
- 万恶的mysql deadlocks
https://github.com/aneasystone/mysql-deadlocks/blob/master/11.md https://blog.csdn.net/dhfzhishi/art ...
- android-auto-scroll-view-pager (无限广告轮播图)
github 地址: https://github.com/Trinea/android-auto-scroll-view-pager Gradle: compile ('cn.trinea.andr ...
- 并发之AtomicInteger
并发之AtomicInteger 1 java.util.concurrent.atomic概要 在java.util.concurrent.atomic包下存在着18个类,其中Integer ...
- dubbo-admin打包和zookper安装
1 首选安装Zookper,下载zookeeper-3.5.3-beta版本,在这里我主要演示这个:下载地址:http://mirrors.hust.edu.cn/apache/zookeeper/ ...
- .NET回归 HTML----超文本标记语言(暂时无图)
HTML用来做网页,文件拓展名改为html可以形成网页 超文本标记语言==超越了文字的范畴,除了文字还可以有图片.视频.音频.动画特效等其它内容,由标记符号<>组成的一门计算机编程语言 H ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- oracle数据库输入conn / as sysdba 出现ORA-01031: insufficient privileges + 忘记sys密码如何改密码
今天忘记了oracle数据库sys用户的密码,想着直接改密码输入conn / as sysdba 出现了ORA-01031: insufficient privileges(权限不足)的错误,到处搜教 ...
- 612D The Union of k-Segments
传送门 题目大意 给定n个区间,问你被覆盖至少k次的区间(两端连续区间可以合并)最少有多少个,并输出. 分析 大水题呀QwQ,只需要将每个点的位置及它是左端点还是右端点这两个信息存起来然后进行一些简单 ...
- WOJ 46 完全背包
高级的暴力,神仙优化…… 首先$O(n^{3})$的$dp$很好想,然后这样可以$O(1)$地回答询问. 考虑到所有物品的体积是一个连续的区间,所以说我们可以合并一些物品来达到预处理时间均摊的效果. ...
- 树莓派研究笔记(2)-- 安装Nginx 服务器,PHP 和 SQLite
1. 安装nginx web 服务器 sudo apt-get install nginx 2. 启动nginx,nginx的www目录默认在/usr/share/nginx/html中 sudo / ...