题目描述

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

输出格式:

  • 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. 6
  2. 3
  3. 2
  4. 6
  5. 1
  6. 1
  7. 2
输出样例#1:

  1. 3
  2. 3
  3. 0
  4. 6
  5. 6
  6. 0

说明

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.

栈。

屠龙宝刀点击就送

  1. #include <cstdio>
  2. #define N 100005
  3. int a[N],num[N],ans[N],n,last,stack[N],top;
  4. int main()
  5. {
  6. scanf("%d",&n);
  7. for(int i=;i<=n;i++)
  8. {
  9. scanf("%d",&a[i]);num[i]=i;
  10. if(!top) {stack[++top]=i;continue;}
  11. else if(a[stack[top]]<a[i])
  12. {
  13. for(;(a[stack[top]]<a[i])&&top;top--)
  14. ans[num[stack[top]]]=i;
  15. }
  16. stack[++top]=i;
  17. }
  18. for(int i=;i<=n;i++) printf("%d\n",ans[i]);
  19. return ;
  20. }

洛谷 P2947 [USACO09MAR]仰望Look Up的更多相关文章

  1. 洛谷P2947 [USACO09MAR]仰望Look Up

    P2947 [USACO09MAR]仰望Look Up 74通过 122提交 题目提供者洛谷OnlineJudge 标签USACO2009云端 难度普及/提高- 时空限制1s / 128MB 提交   ...

  2. 洛谷 P2947 [USACO09MAR]向右看齐Look Up【单调栈】

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

  3. 洛谷 P2947 [USACO09MAR]向右看齐Look Up

    目录 题目 思路 \(Code\) 题目 戳 思路 单调栈裸题 \(Code\) #include<stack> #include<cstdio> #include<st ...

  4. 洛谷P2947 [USACO09MAR]向右看齐Look Up

    #include<cstdio> #include<algorithm> #include<stack> #include<cctype> using ...

  5. 【洛谷P2947】向右看齐

    向右看齐 题目链接 此题可用单调栈O(n)求解 维护一个单调递减栈,元素从左到右入栈 若新加元素大于栈中元素,则栈中元素的仰望对象即为新加元素 每次将小于新加元素的栈中元素弹出,记录下答案 #incl ...

  6. 洛谷 P2945 [USACO09MAR]沙堡Sand Castle 题解

    题目传送门 大概思路就是把这两个数组排序.在扫描一次,判断大小,累加ans. #include<bits/stdc++.h> using namespace std; int x,y,z; ...

  7. 洛谷2944 [USACO09MAR]地震损失2Earthquake Damage 2

    https://www.luogu.org/problem/show?pid=2944 题目描述 Wisconsin has had an earthquake that has struck Far ...

  8. 洛谷 P2945 [USACO09MAR]沙堡Sand Castle

    传送门 题目大意: ai,ai+1,ai+2... 变成 bi,bi+1,bi+2.. 不计顺序,增加和减少a数组均有代价. 题解:贪心+排序 小的对应小的 代码: #include<iostr ...

  9. 洛谷2943 [USACO09MAR]清理Cleaning Up——转变枚举内容的dp

    题目:https://www.luogu.org/problemnew/show/P2943 一下想到n^2.然后不会了. 看过TJ之后似乎有了新的认识. n^2的冗余部分在于当后面那部分的种类数一样 ...

随机推荐

  1. 在xshell中使用Linux语言打开错误提示

    上线项目到服务器后, 有时候有的功能跟本地调试的不一样,这时候就需要设置打开display_errors = On: 首先,cd .. 进入上一级,ll 罗列当前目录,跟home当前目录的有这个usr ...

  2. Hadoop 源代码组织结构

    Hadoop 2.X 包括 编译好的可以直接部署的文件hadoop-{VERSION}.tar.gz; 还有源代码文件hadoop-{VERSION}-src.tar.gz , 需要 Maven 编译 ...

  3. 1.5 Hive初步使用和安装MySQL

    一.HQL初步试用 1.创建一个student表 #创建一个student表 hive> create table student(id int, name string) ROW FORMAT ...

  4. QDUOJ 一道简单的数据结构题 栈的使用(括号配对)

    一道简单的数据结构题 发布时间: 2017年6月3日 18:46   最后更新: 2017年6月3日 18:51   时间限制: 1000ms   内存限制: 128M 描述 如果插入“+”和“1”到 ...

  5. C#总结---方法的out参数和ref参数

    我们知道,在c#中,当我们在一个方法中想要访问另一个方法中的变量的时候,有两种解决方案---参数和返回值.但当需要返回多个值,并且是不同类型的值的之后应该怎么办呢?解决方案可以是 (1)将所有类型数据 ...

  6. E20180603-hm

    breadth  n. 宽度; 宽容; (知识.兴趣等的) 广泛; depth  n. 深度; 深处,深海; 深奥,奥妙; 进深; deep adj. 深的; 深远的,深奥; 重大的,深刻的; 强烈的 ...

  7. lightoj1060【康托逆展开】

    可以先看些资料:http://blog.csdn.net/keyboarderqq/article/details/53388936 参考谷巨巨:http://blog.csdn.net/azx736 ...

  8. 51nod 1102 【单调栈】

    思路: 对于这个高度往左能延伸最远x,往右能延伸最远y,(x+1+y)*w; 利用单调栈就行了: #include <cstdio> #include <stack> #inc ...

  9. HDU5920【模拟】

    模拟题这种东西啊~就是自己读题,自己打,没有别的方法...贴份6000+b的code跑: #include <bits/stdc++.h> using namespace std; //t ...

  10. C#主从表查询

    软件的使用必然涉及到主表和子表的操作,我们先在SQLite中创建子表.比如 创建一学生信息表做主表,再创建一个学生成绩表做子表.然后我们在程序中成绩 方法来连接子表. 判断bindingsource中 ...