codeforces --- 279C Ladder
2 seconds
256 megabytes
standard input
standard output
You've got an array, consisting of n integers a1, a2, ..., an. Also, you've got m queries, the i-th query is described by two integers li, ri. Numbers li, ri define a subsegment of the original array, that is, the sequence of numbers ali, ali + 1, ali + 2, ..., ari. For each query you should check whether the corresponding segment is a ladder.
A ladder is a sequence of integers b1, b2, ..., bk, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer x (1 ≤ x ≤ k), that the following inequation fulfills: b1 ≤ b2 ≤ ... ≤ bx ≥ bx + 1 ≥ bx + 2... ≥ bk. Note that the non-decreasing and the non-increasing sequences are also considered ladders.
The first line contains two integers n and m (1 ≤ n, m ≤ 105) — the number of array elements and the number of queries. The second line contains the sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 109), where number ai stands for the i-th array element.
The following m lines contain the description of the queries. The i-th line contains the description of the i-th query, consisting of two integers li, ri (1 ≤ li ≤ ri ≤ n) — the boundaries of the subsegment of the initial array.
The numbers in the lines are separated by single spaces.
Print m lines, in the i-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the i-th query is the ladder, or word "No" (without the quotes) otherwise.
8 6
1 2 1 3 3 5 2 1
1 3
2 3
2 4
8 8
1 4
5 8
Yes
Yes
No
Yes
No
Yes 思路:dp[i]表示a[i]之前连续的比a[i]大的数的个数,rdp[i]表示a[i]之后连续的比a[i]大的数的个数。如果dp[st] + rdp[end] >= end - st + 1,则是Yes,否则No。
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 100005
using namespace std;
int a[MAX], dp[MAX], rdp[MAX];
int main(){
int n, Q, st, end;
/* freopen("in.c", "r", stdin); */
while(~scanf("%d%d", &n, &Q)){
memset(dp, , sizeof(dp));
memset(rdp, , sizeof(rdp));
memset(a, , sizeof(a));
for(int i = ;i <= n;i ++) scanf("%d", &a[i]);
for(int i = ; i <= n;i ++){
if(a[i] <= a[i-]) dp[i] = dp[i-] + ;
else dp[i] = ;
}
for(int i = n;i >= ;i --){
if(a[i] <= a[i+]) rdp[i] = rdp[i+] + ;
else rdp[i] = ;
}
for(int i = ;i < Q;i ++){
scanf("%d%d", &st, &end);
if(rdp[st] + dp[end] >= end - st + ) printf("Yes\n");
else printf("No\n");
}
}
return ;
}
codeforces --- 279C Ladder的更多相关文章
- Codeforces 279C - Ladder - [简单DP]
题目链接:http://codeforces.com/problemset/problem/279/C 题意: 给出 $n$ 个整数 $a[1 \sim n]$,$m$ 个查询,对于一个查询 $[l_ ...
- CodeForces 279C Ladder (RMQ + dp)
题意:给定一个序列,每次一个询问,问某个区间是不是先增再降的. 析:首先先取处理以 i 个数向左能延伸到哪个数,向右能到哪个数,然后每次用RQM来查找最大值,分别向两边延伸,是否是覆盖区间. 代码如下 ...
- 【Codeforces 279C】Ladder
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 设pre[i]表示i往前一直递增能递增多远 设aft[i]表示i往后一直递增能递增多远 如果aft[l]+pre[r]>=(r-l+1) ...
- Codeforces 801 A.Vicious Keyboard & Jxnu Group Programming Ladder Tournament 2017江西师大新生赛 L1-2.叶神的字符串
A. Vicious Keyboard time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
随机推荐
- IOS-开发日志-UIScrollView
UIScrollView 1. contentOffset 默认CGPointZero,用来设置scrollView的滚动偏移量. // 设置scrollView的滚动偏移量 scrollView. ...
- 我眼中真正优秀的CTO
该文转自“肉饼铺子”.作者robbin是前JavaEye网站的创始人,TOPITCLUB互联网俱乐部发起人. 原文链接 现在进入正题,最近几个月,不断有人找我推荐CTO人选,这两年互联网创业和创投实 ...
- UVA 11300 Spreading the Wealth (数学推导 中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...
- What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?
转自: http://www.codeproject.com/Articles/76252/What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc 解释的超详细!! ...
- 排序算法——QuickSort、MergeSort、HeapSort(C++实现)
快速排序QuickSort template <class Item> void quickSort (Item a[], int l, int r) { if (r<=l) ret ...
- ODBC方式连接Informix数据库
公司某个报表系统使用Informix数据库,在谋划使用Perl语言写数据采集程序后,花费了很多时间建立Perl访问Informix连接.恰巧Windows下ActivePerl的CPAN中又没有DBD ...
- VHDL程序的库
VHDL库存储和放置了可被其他VHDL程序调用的数据定义.器件说明.程序包等资源.VHDL库的种类有很多,但最常见的库有IEEE标准库.WORK库.IEEE标准库主要包括STD_LOGIC_1164. ...
- 从IT的角度思考BIM(三):敏捷开发
人们看到了远处BIM的美丽胜景和阻挡在眼前的宽广河流.有些人自信满满地跳入河中打算孤身游过彼岸,可是却失败了.有些人匆匆忙忙地造了船胡乱地滑向彼岸,可是也失败了. 要如何继续这段探索之旅? 无论是&l ...
- ipad ------ 与iPhone的差别
1. 差异 iPhone是手机,iPad.iPad Mini是平板电脑 iPhone和iPad开发的区别 屏幕的尺寸 \分辨率 UI元素的排布 \设计 键盘 API 屏幕方向的支持 … … 2. iP ...
- android SurfaceView绘制 重新学习--切图clipRect详解
解释都在代码注释中: public class SampleView extends View { private Paint mPaint; private Path mPath; public S ...