题目描述

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。

输入输出样例

输入样例#1:

6

3

2

6

1

1

2

输出样例#1:

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.

【输入说明】6 头奶牛的身高分别为 3, 2, 6, 1, 1, 2.

【输出说明】奶牛#1,#2 仰望奶牛#3,奶牛#4,#5 仰望奶牛#6,奶牛#3 和#6 没有仰望对象。

【数据规模】

对于 20%的数据: 1≤N≤10;

对于 50%的数据: 1≤N≤1,000;

对于 100%的数据:1≤N≤100,000;1≤H_i≤1,000,000;

【分析】:

首先能不能随便一点,我就是维护一个栈的单调递增(不管三七二十一),那么我要如何利用呢?

① 3进栈;

② 2比栈顶元素小,不要;

③ 6比栈顶元素大,进去;

④ 。。。。。等等窝们不是要求一个元素比他的最近么?这样子2都不要了。。然后6过来了还进栈。。这不是背道而驰了。

所以方案错误;

那么不是维护单调递增,就是维护一下单调递减呗;

① 3进栈

② 2比3小进栈

③ 6比2大,咦?一下就是6比2大,而且后面都没有碰到,所以6一定是2的单侧最近,然后2出栈,并且2的答案就是6元素的位置。然后看3,3还是比6小,OK,满足。最后把6进栈。

④ 1进栈

⑤ 1进栈

⑥ 2的时候,栈里面的两个1出栈,然后2进栈

⑦ 最后注意,栈里面还有6和2这两个元素,可惜没有他们的答案,那就是0;

【代码】:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 1000005
#define mod 10007
#define eps 1e-5
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int n;
struct node
{
int num,pos;
}tmp;
stack<node> st;
int ans[maxn];
int a[maxn];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
tmp.num=a[1];
tmp.pos=1;
st.push(tmp);
for(int i=2;i<=n;i++)
{
while(!st.empty() && a[i]>st.top().num){
ans[st.top().pos]=i;
st.pop();
}
tmp.num=a[i];
tmp.pos=i;
st.push(tmp);
}
while(!st.empty())
{
ans[st.top().pos]=0;
st.pop();
}
for(int i=1;i<=n;i++)
{
printf("%d\n",ans[i]);
}
}

【手写栈】

#include<bits/stdc++.h>
using namespace std;
int f[1501],f2[1501][1501],b[100001],top=1,n,x,m;
struct aaa {int num,data;}a[100001],filo[100001];//用结构体来实现,num为序号,data为身高
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].data;
a[i].num=i; //边读边做
for(int j=top;j>=1;j--)
if(filo[top].data<a[i].data)b[filo[top].num]=a[i].num,top--;//判断,记录,出栈
filo[++top]=a[i];//压栈
}
for(int i=1;i<=n;i++)cout<<b[i]<<endl;//输出!
return 0;//华丽丽的结束!
}

洛谷 P2947 [USACO09MAR]向右看齐Look Up【单调栈】的更多相关文章

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

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

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

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

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

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

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

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

  5. 【洛谷P2947】向右看齐

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

  6. [BZOJ 3039&洛谷P4147]玉蟾宫 题解(单调栈)

    [BZOJ 3039&洛谷P4147]玉蟾宫 Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地. ...

  7. 【洛谷P1823】音乐会的等待 单调栈+二分

    题目大意:给定一个长度为 N 的序列,定义两个数 \(a[i],a[j]\) 相互看得见,意味着 \(\forall k\in [i+1,j-1],a[k]\le a[i],a[k]\le a[j]\ ...

  8. 洛谷P2659 美丽的序列 单调栈模板

    P2659 美丽的序列 题目链接 https://www.luogu.org/problemnew/show/P2659 题目描述 为了研究这个序列的美丽程度,GD定义了一个序列的"美丽度& ...

  9. 题解【洛谷P5788】【模板】单调栈

    题面 单调栈模板题. 单调栈与单调队列一样,都是维护了一段区间内的顺序. 然后--这个题用一个栈维护一下贪心就没了. 具体参考这一篇题解 #include <bits/stdc++.h> ...

随机推荐

  1. SpringBoot打war包并部署到tomcat下运行

    一.修改pom.xml. 1.packaging改为war 2.build节点添加<finalName>你的项目名</finalName> 二.修改项目启动类,继承Spring ...

  2. Bzoj4873 [SXOI2017]寿司餐厅

    Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 64  Solved: 45 Description Kiana最近喜欢到一家非常美味的寿司餐厅用餐.每 ...

  3. DotNet 学习笔记 OWIN

    Open Web Interface for .NET (OWIN) ----------------------------------------------------------------- ...

  4. CSS 竖线颜色渐变

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"& ...

  5. linux下删除已经不用的配置文件

    使用命令 dpkg -l | grep -v ^ii 查看当前未安装或者不用了的配置文件 例如我的显示如下

  6. 【转】jpeg文件格式详解

    JPEG(Joint Photographic Experts Group)是联合图像专家小组的英文缩写.它由国际电话与电报咨询委员会CCITT(The International Telegraph ...

  7. map,set的底层实现:红黑树[多图,手机慎入]

    最近天下有一种颇不太平的感觉,各地的乱刀砍人,到处是贪官服法.京东准备上市了,阿里最近也提交申请了,猎豹也逆袭了,据说猎豹移动在国际市场上表现甚是抢眼.只有屌丝还在写着代码.花开花又谢,花谢花又开,为 ...

  8. linux编程之消息队列

    消息队列是内核地址空间中的内部链表,通过linux内核在各个进程之间传递内容,消息顺序地发送到消息队列中,并且以几种不同的方式 从队列中获取,每个消息队列可以用IPC标识符唯一的进行标识,内核中的消息 ...

  9. abp 调试

    概要 研究Abp(ASP.NET Boilerplate)框架有几个月了,从一遍遍的看官方文档,到现在看源码,一路走来学习了很多知识. 很多新手都很关心源码如何调试,我也是如此,在反复看Debuggi ...

  10. SqlServer存储过程中使用事务,示例

    create proc pro_GetProTrans @GoodsId int, @Number int, @StockPrice money, @SupplierId int, @EmpId in ...