【BZOJ3401】【单调栈】[Usaco2009 Mar]Look Up 仰望 题解

本文详细解析了Usaco2009年3月比赛中的Look Up问题,利用单调栈解决奶牛身高比较的问题,介绍了如何在O(N)的时间复杂度内找出每头奶牛能看到的奶牛数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

约翰的N(1≤N≤105)头奶牛站成一排,奶牛i的身高是Hi(l≤Hi≤1,000,000).现在,每只奶牛都在向后看齐.对
于奶牛i,如果奶牛j满足i

#include <bits/stdc++.h>
#define LL long long
#define clr(x) memset(x, 0, sizeof x)
#define ms(a, x) memset(x, a, sizeof x)
#define digit (ch <  '0' || ch >  '9')

using namespace std;

template <class T> inline void read(T &x) {
    int flag = 1; x = 0;
    register char ch = getchar();
    while( digit) { if(ch == '-')  flag = -1; ch = getchar(); }
    while(!digit) { x = (x<<1)+(x<<3)+ch-'0'; ch = getchar(); }
    x *= flag;
}

const int maxn = 1e5+5;
int n,a[maxn],sta[maxn],top,ans[maxn];

int main () {
    read(n);
    for(int i = 1; i <= n; i++) read(a[i]);
    for(int i = n; i >= 1; i--) {
        while(a[sta[top]] <= a[i] && top) top--;
        ans[i] = sta[top]; sta[++top] = i;
    }
    for(int i = 1; i <= n; i++) printf("%d\n",ans[i]);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值