https://www.luogu.org/problem/P2947

题目描述

Farmer John's N (1 <= N <= 100,000) cows, conveniently numbered 1..N, are once again standing in a row. Cow i has height Hi (1 <= Hi <= 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 Hi < Hj. 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.

输入描述:

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains the single integer: Hi

输出描述:

* 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.

示例1

输入

  1.  

输出

  1.  

说明

FJ has six cows of heights 3, 2, 6, 1, 1, and 2.
Cows 1 and 2 both look up to cow 3; cows 4 and 5 both look up to cow 6; and cows 3 and 6 do not look up to any cow.

找每个数右边第一个大于等于它的位置,并输出该位置,如果没有就输出0

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <math.h>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <queue>
  9. #include <set>
  10. #include <map>
  11. #include <math.h>
  12. const int INF=0x3f3f3f3f;
  13. typedef long long LL;
  14. const int mod=1e9+;
  15. const int maxn=1e5+;
  16. using namespace std;
  17.  
  18. int A[maxn];
  19. int ans[maxn];
  20.  
  21. int main()
  22. {
  23. int n;
  24. scanf("%d",&n);
  25. for (int i=;i<=n;i++)
  26. scanf("%d",&A[i]);
  27. vector<int> vt;
  28. vt.push_back();
  29. int MIN=A[];
  30. for (int i = ; i <= n; i++)
  31. {
  32. if(A[i]>=MIN)
  33. {
  34. while (!vt.empty())
  35. {
  36. if (A[*(vt.end()-)]<A[i])
  37. {
  38. ans[*(vt.end()-)]=i;
  39. vt.erase(vt.end()-);
  40. }
  41. else
  42. break;
  43. }
  44. vt.push_back(i);
  45. }
  46. else
  47. {
  48. vt.push_back(i);
  49. MIN=ans[i];
  50. }
  51. }
  52. for (vector<int>::iterator it=vt.begin();it!=vt.end();it++)
  53. {
  54. ans[*it]=;
  55. }
  56. for (int i=;i<=n;i++)
  57. printf("%d\n",ans[i]);
  58. return ;
  59. }

其实上面的代码就用到了单调栈的思想

何为单调栈?
解释一下:
单调栈类似单调队列,这道题中要运用到单调栈。
如下:
令f[i]为向右看齐的人的标号
6 3 2 6 1 1 2
f分别为 3 3 0 6 6 0
首先,最后一个人必然没有向右看齐的人的编号
先将最右边的人加入栈
接着,我们发现1,1比2小,先加入栈,当前f值为6
接着,又来了一个1,发现1=1,弹出1,接着发现1<2,则将1加进栈,当前f值为6
接着,来了一个6,6>2,弹出2,当前f值为0
接着,来了一个2,2<6,加入2,当前f值为3
最后,来了一个3,3>2,弹出2,将3加入栈,当前f值为3
最后的栈中有3和6
最后的答案就是3 3 0 6 6 0
每次只在栈中存数字标号,进来一个数,就把小于等于他的数全部弹出,若剩下有数,则答案是剩下的数,否则答案是0
为什么?
因为:
若x小于当前数,x不会成为答案。
这就是单调栈的用法

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <math.h>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <stack>
  9. #include <queue>
  10. #include <set>
  11. #include <map>
  12. #include <math.h>
  13. const int INF=0x3f3f3f3f;
  14. typedef long long LL;
  15. const int mod=1e9+;
  16. const int maxn=1e5+;
  17. using namespace std;
  18.  
  19. int A[maxn];
  20. int ans[maxn];
  21. stack<int> sk;
  22.  
  23. int main()
  24. {
  25. int n;
  26. scanf("%d",&n);
  27. for (int i=;i<=n;i++)
  28. scanf("%d",&A[i]);
  29. for(int i=n;i>=;i--)
  30. {
  31. while(!sk.empty()&&A[sk.top()]<=A[i])
  32. sk.pop();
  33. if(!sk.empty())
  34. ans[i]=sk.top();
  35. else
  36. ans[i]=;
  37. sk.push(i);
  38. }
  39. for (int i=;i<=n;i++)
  40. printf("%d\n",ans[i]);
  41. return ;
  42. }

[USACO09MAR]向右看齐Look Up(单调栈、在线处理)的更多相关文章

  1. P2947 [USACO09MAR]向右看齐Look Up--单调栈

    单调栈真的很好用呢! P2947 [USACO09MAR]向右看齐Look Up 题目描述 Farmer John's N (1 <= N <= 100,000) cows, conven ...

  2. luogu P2947 [USACO09MAR]向右看齐Look Up |单调队列

    题目描述 Farmer John's N (1 <= N <= 100,000) cows, conveniently numbered 1..N, are once again stan ...

  3. BZOJ.4540.[HNOI2016]序列(莫队/前缀和/线段树 单调栈 RMQ)

    BZOJ 洛谷 ST表的一二维顺序一定要改过来. 改了就rank1了哈哈哈哈.自带小常数没办法. \(Description\) 给定长为\(n\)的序列\(A_i\).\(q\)次询问,每次给定\( ...

  4. DP的各种优化(动态规划,决策单调性,斜率优化,带权二分,单调栈,单调队列)

    前缀和优化 当DP过程中需要反复从一个求和式转移的话,可以先把它预处理一下.运算一般都要满足可减性. 比较naive就不展开了. 题目 [Todo]洛谷P2513 [HAOI2009]逆序对数列 [D ...

  5. HUID 5558 Alice's Classified Message 后缀数组+单调栈+二分

    http://acm.hdu.edu.cn/showproblem.php?pid=5558 对于每个后缀suffix(i),想要在前面i - 1个suffix中找到一个pos,使得LCP最大.这样做 ...

  6. HDU - 5033 Building (单调栈+倍增)

    题意:有一排建筑,每座建筑有一定的高度,宽度可以忽略,求在某点的平地上能看到天空的最大角度. 网上的做法基本都是离线的...其实这道题是可以在线做的. 对于向右能看到的最大角度,从右往左倍增维护每个时 ...

  7. 2019 计蒜之道 复赛 D. “星云系统”(单调栈)

    VIPKID 是在线少儿英语教育平台,网络稳定是在线教育课程质量的红线,VIPKID 为此推出了全球最稳定的教育网络系统 -- "星云系统".星云系统目前建立了覆盖全球 3535 ...

  8. 2019牛客暑期多校训练营(第一场) - A - Equivalent Prefixes - 单调栈

    A - Equivalent Prefixes - 单调栈 题意:给定两个n个元素的数组a,b,它们的前p个元素构成的数组是"等价"的,求p的最大值."等价"的 ...

  9. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

随机推荐

  1. java处理浮点数小数点后几位

    转载:https://blog.csdn.net/xue_feitian/article/details/6556275 第一种方法: double f = 123.2315455458; BigDe ...

  2. 使用Redis SortedSet实现增量更新

    导读:前段时间有个需求是提供一个接口供客户端增量更新数据,当有数据被删除了以后客户端也需要感知到,并且要支持一定并发: 关键词:高并发,增量更新 前言 何谓增量更新,顾名思义就是只更新变化的部分,这样 ...

  3. HTML元素类型和类型的转换

    HTML元素分为:块状元素和内联元素 块元素:(block) 1.默认独占一行 2.没有宽度时,默认撑满一排 3.可以定义元素的宽和高 常见的块状元素有div,ul,li,h1-h6,ol 内联,行内 ...

  4. Centos7.4系统 httpd模式搭建文件服务器

    环境:服务环境:centos7.4 说明:搭建Apache文件服务器,下载路径为/opt/ymyg(下载路径根据实际需要自己定义) 步骤: 1.安装httpd服务   [root@localhost ...

  5. Linux学习-第二章(命令)20200216

  6. 每天一点点之vue框架开发 - axios解决跨越问题

    1.安装 npm install axios 或者 使用 bower: bower install axios 或者直接使用 cdn: <script src="https://unp ...

  7. 用户使用API函数对创建的文件进行读写操作

    HANDLE handle; //定义文件句柄 ]; //定义缓冲区 int i; //接收实际操作的字节数 CString str; //定义字符串变量 handle = ::CreateFile( ...

  8. linux下springboot项目通过jetty发布war包应用

    Linux下jetty发布jar包 1.通过官网下载jetty点击下载https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-distributi ...

  9. XML技术详解

    XML 1.XML概述 XML可扩展标记语言是一种基于文本的语言用作应用程序之间的通信模式,是一个非常有用的描述结构化信息的技术.XML工具使得转化和处理数据变得十分容易,但同样也要领域相关的标准和代 ...

  10. 最小二乘拟合(scipy实现)

    Scipy库在numpy库基础上增加了众多数学,科学及工程计算中常用库函数.如线性代数,常微分方程数值求解,信号处理,图像处理,稀疏矩阵等. 如下理解通过Scipy进行最小二乘法拟合运算 最小二乘拟合 ...