洛谷 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
输出格式:
- 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.
输入输出样例
6
3
2
6
1
1
2
3
3
0
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.
栈。
#include <cstdio>
#define N 100005
int a[N],num[N],ans[N],n,last,stack[N],top;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);num[i]=i;
if(!top) {stack[++top]=i;continue;}
else if(a[stack[top]]<a[i])
{
for(;(a[stack[top]]<a[i])&⊤top--)
ans[num[stack[top]]]=i;
}
stack[++top]=i;
}
for(int i=;i<=n;i++) printf("%d\n",ans[i]);
return ;
}
洛谷 P2947 [USACO09MAR]仰望Look Up的更多相关文章
- 洛谷P2947 [USACO09MAR]仰望Look Up
P2947 [USACO09MAR]仰望Look Up 74通过 122提交 题目提供者洛谷OnlineJudge 标签USACO2009云端 难度普及/提高- 时空限制1s / 128MB 提交 ...
- 洛谷 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 ...
- 洛谷P2947 [USACO09MAR]向右看齐Look Up
#include<cstdio> #include<algorithm> #include<stack> #include<cctype> using ...
- 【洛谷P2947】向右看齐
向右看齐 题目链接 此题可用单调栈O(n)求解 维护一个单调递减栈,元素从左到右入栈 若新加元素大于栈中元素,则栈中元素的仰望对象即为新加元素 每次将小于新加元素的栈中元素弹出,记录下答案 #incl ...
- 洛谷 P2945 [USACO09MAR]沙堡Sand Castle 题解
题目传送门 大概思路就是把这两个数组排序.在扫描一次,判断大小,累加ans. #include<bits/stdc++.h> using namespace std; int x,y,z; ...
- 洛谷2944 [USACO09MAR]地震损失2Earthquake Damage 2
https://www.luogu.org/problem/show?pid=2944 题目描述 Wisconsin has had an earthquake that has struck Far ...
- 洛谷 P2945 [USACO09MAR]沙堡Sand Castle
传送门 题目大意: ai,ai+1,ai+2... 变成 bi,bi+1,bi+2.. 不计顺序,增加和减少a数组均有代价. 题解:贪心+排序 小的对应小的 代码: #include<iostr ...
- 洛谷2943 [USACO09MAR]清理Cleaning Up——转变枚举内容的dp
题目:https://www.luogu.org/problemnew/show/P2943 一下想到n^2.然后不会了. 看过TJ之后似乎有了新的认识. n^2的冗余部分在于当后面那部分的种类数一样 ...
随机推荐
- Linux命令行设置环境变量
参考 Linux命令行--使用linux环境变量 Linux命令行—使用Linux环境变量
- set和multiset容器
set和multiset容器的能力 set 和multiset容器的内部结构通常由平衡二叉树(balanced binary tree)来实现.当元素放入容器中时,会按照一定的排序法则自动排序,默认是 ...
- Oracle中的集合运算
前言:在实际项目中对多表进行集合运算使用非常广泛,以下是对集合操作的部分总结. 一,建表,插入测试数据 create table a( numbers integer ) create table b ...
- 剑指offer面试题:输入某二叉树的前序遍历和中序遍历,输出后序遍历
二叉树的先序,中序,后序如何遍历,不在此多说了.直接看题目描述吧(题目摘自九度oj剑指offer面试题6): 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结 ...
- 51nod1181【素数筛】
思路: 直接就是筛出素数,然后我很撒比的从那个地方往后for找一个位置也是质数的输出: #include <bits/stdc++.h> using namespace std; type ...
- bzoj 2738: 矩阵乘法【整体二分+树状数组】
脑子一抽开始写主席树,敲了一会发现不对-- 整体二分,用二维树状数组维护值为当前区间的格子个数,然后根据k的大小和当前询问的子矩阵里的值和k的大小关系来决定这个询问放在哪一部分向下递归 #includ ...
- shell 截取字符串(转)
linux中对字符串的处理: 1.字符串分割例如 AAAAA-BBBBBB 按-分割去前后两部分 cut : [rich@localhost ~]$ str=AAAAA-BBBBBB[rich@l ...
- native-echarts 在安卓上无法显示出来
1.native-echarts 的配置是百度echarts 2.模拟器上试了很多次都显示不出来(具体不清楚,我的是这样) 3.真机测试可以显示图表,以下是配置: a.将node_modules\na ...
- nutzboot dubbo zookeeper简单使用
提供方和消费方properties 配置基本差不多 nutz.application.name这个值不一样 提供方配置自动端口就行server.port=0 消费方一般需要对外提供web服务配置ip和 ...
- B.出题人的女装
链接:https://ac.nowcoder.com/acm/contest/358/B 题意: 出题人早上起床就打算穿衣服,他有两箱衣服,因为懒,他在这两天只打算打开一个箱子. 两个箱子中一个有n件 ...