luogu P2947 [USACO09MAR]向右看齐Look Up |单调队列
题目描述
Farmer John's N (1 <= N <= 100,000) cows, conveniently numbered 1..N, are once again standing in a row. Cow i has height H_i (1 <= H_i <= 1,000,000).
Each cow is looking to her left toward those with higher index numbers. We say that cow i 'looks up' to cow j if i < j and H_i < H_j. For each cow i, FJ would like to know the index of the first cow in line looked up to by cow i.
Note: about 50% of the test data will have N <= 1,000.
约翰的N(1≤N≤10^5)头奶牛站成一排,奶牛i的身高是Hi(l≤Hi≤1,000,000).现在,每只奶牛都在向右看齐.对于奶牛i,如果奶牛j满足i<j且Hi<Hj,我们可以说奶牛i可以仰望奶牛j. 求出每只奶牛离她最近的仰望对象.
Input
输入格式
Line 1: A single integer: N
Lines 2..N+1: Line i+1 contains the single integer: H_i
第 1 行输入 N,之后每行输入一个身高 H_i。
输出格式
- Lines 1..N: Line i contains a single integer representing the smallest index of a cow up to which cow i looks. If no such cow exists, print 0.
共 N 行,按顺序每行输出一只奶牛的最近仰望对象,如果没有仰望对象,输出 0。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5+10;
int a[N],q[N],ans[N];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
q[1]=1;
int l=1,r=1;
for(int i=1;i<=n;i++){
while(r>=l&&a[q[r]]<a[i]){
ans[q[r]]=i;
r--;
}
q[++r]=i;
}
for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
}
luogu P2947 [USACO09MAR]向右看齐Look Up |单调队列的更多相关文章
- 【luogu P2947 [USACO09MAR]向右看齐Look Up】 题解
题目链接:https://www.luogu.org/problemnew/show/P2947 因为在单调队列上被dalao们锤爆 怒刷单调队列题 何为单调队列? 设我们的队列为从左至右单调递增 对 ...
- P2947 [USACO09MAR]向右看齐Look Up--单调栈
单调栈真的很好用呢! P2947 [USACO09MAR]向右看齐Look Up 题目描述 Farmer John's N (1 <= N <= 100,000) cows, conven ...
- [luogu P1776] 宝物筛选 解题报告(单调队列优化DP)
题目链接: https://www.luogu.org/problemnew/show/P1776 题目: 终于,破解了千年的难题.小FF找到了王室的宝物室,里面堆满了无数价值连城的宝物……这下小FF ...
- Luogu 2216[HAOI2007]理想的正方形 - 单调队列
Solution 二维单调队列, 这个数组套起来看得我眼瞎... Code #include<cstdio> #include<algorithm> #include<c ...
- 【Luogu】P1419寻找段落(单调队列)
题目链接 不知为何状态突然奇差无比,按说这题本来应该是水题的,但不仅不会做,还比着题解爆零五次 二分平均值(想到了),单调队列维护最大区间和(想到了但是不会,???为什么我不会???) #includ ...
- 【Luogu】P2219修筑绿化带(单调队列)
题目链接 这题各种边界判断恶心死人 就是单调队列在每行求出最小的.能装进A*B方块里的花坛 然后再在刚刚求出的那个东西里面跑一遍竖着的单调队列 然后……边界调了一小时 做完这题我深刻地感觉到我又强了 ...
- [luogu]P3572 [POI2014]PTA-Little Bird(单调队列)
P3572 [POI2014]PTA-Little Bird 题目描述 In the Byteotian Line Forest there are nn trees in a row. On top ...
- 洛谷 P2947 [USACO09MAR]向右看齐Look Up【单调栈】
题目描述 Farmer John's N (1 <= N <= 100,000) cows, conveniently numbered 1..N, are once again stan ...
- 洛谷 P2947 [USACO09MAR]向右看齐Look Up
目录 题目 思路 \(Code\) 题目 戳 思路 单调栈裸题 \(Code\) #include<stack> #include<cstdio> #include<st ...
随机推荐
- starUML 3.0.2文件 百度云
文件百度云---链接:https://pan.baidu.com/s/1BVX9Ze40bkgd3k_4WFb99A 提取码:t7kj 使用方法:将starUML安装目录(一般为 C:\Program ...
- C语言I博客作业08
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 作业 我在这个课程的目标是 熟悉使用while和do-while结构解决问题 这个作业在那个具体方面帮助我实现目标 pta题目及查找的资料 ...
- Spring源码解析之@Configuration
@Configuration简介 用于标识一个类为配置类,与xml配置效果类似 用法简介 public class TestApplication { public static void main( ...
- Junit使用方法
Junit使用方法 步骤: 定义 一个测试类(测试用例) 建议 测试类名:被测试类名+Test 包名:xxx.xxx.xxx.test 测试方法 建议: 方法名:test测试的方法名 返回值: voi ...
- 创建基于OData的Web API - Knowledge Builder API, Part IV: Write Controller
基于上一篇<创建基于OData的Web API - Knowledge Builder API, Part III:Write Model and Controller>,新创建的ODat ...
- PHP队列的实现详细操作步骤
队列是一种特殊的线性表,它只允许在表的前端,可以称之为front,进行删除操作:而在表的后端,可以称之为rear进行插入操作.队列和堆栈一样,是一种操作受限制的线性表,和堆栈不同之处在于:队列是遵循“ ...
- 一、netcore跨平台之 Linux上部署netcore和webapi
这几天闲着的时候在linux上部署了一下netcore webapi,下面就纪要一下这个过程. 中间遇到不少的坑,心里都是泪啊. 话不多说,开始干活. ------------------------ ...
- Laravel上传文件(单文件,多文件)
为了方便,先修改一个配置文件,再laravel框架中config配置中找到 filesystems.php 文件 修改代码如下 'local' => [ 'driver' => 'loc ...
- P0-Logisim简单部件与有限状态机
#自学了6week,pre都挂了,做了做P0课下测试,觉得自己对有限状态机概念的的理解,特别是牵扯到时序还是很模糊:状态的抽象也不够熟练:logisim和Verilog的实现也存在问题.网上针对性的l ...
- hdu 5495 LCS (置换群)
Sample Input231 2 33 2 161 5 3 2 6 43 6 2 4 5 1 Sample Output24 C/C++: #include <map> #includ ...