题目描述

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. mybatis中association和collection的column传入多个参数值

    在使用 association和collection 进行关联查询的时候 column 参数可能会有多个,如下: 注意: parameterType 一定要是 java.util.Map

  2. java加载驱动

    加载驱动方法 1.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 2. DriverManager.r ...

  3. vector基础

    //STL基础 //容器 //vector #include "iostream" #include "cstdio" #include "vecto ...

  4. Codeforces 671B/Round #352(div.2) D.Robin Hood 二分

    D. Robin Hood We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and ...

  5. 用python爬校花网

    import requests import re import hashlib,time def get_index(url): response=requests.get(url) if resp ...

  6. c# “XXX::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。

    症状描述如下: 如果将一个委托作为函数指针从托管代码封送到非托管代码,并且在对该委托进行垃圾回收后对该函数指针发出了一个回调,则将激活 callbackOnCollectedDelegate 托管调试 ...

  7. VM 脚本回快照和开关机

    #Import PowerCLI*Get-Module -ListAvailable PowerCLI* | Import-Module #Resolve login issueSet-PowerCL ...

  8. 【洛谷 P1707】 刷题比赛 (矩阵加速)

    题目连接 很久没写矩阵加速了,复习一下,没想到是一道小毒瘤题. 状态矩阵\(a[k],b[k],c[k],a[k+1],b[k+1],c[k+1],k,k^2,w^k,z^k,1\) 转移矩阵 0, ...

  9. DotNETCore 学习笔记 WebApi

    API Description Request body Response body GET /api/todo Get all to-do items None Array of to-do ite ...

  10. hdu 2962 Trucking (二分+最短路Spfa)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others ...