洛谷 P2947 [USACO09MAR] Look Up S (Java)

洛谷 P2947 [USACO09MAR] Look Up S 单调栈 (Java)

传送门:P2947 [USACO09MAR] Look Up S

题目: [USACO09MAR] Look Up S

题目描述

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 ≤ 1 0 5 ) N(1\le N\le10^5) N(1N105) 头奶牛站成一排,奶牛 i i i 的身高是 H i ( 1 ≤ H i ≤ 1 0 6 ) H_i(1\le H_i\le10^6) Hi(1Hi106)。现在,每只奶牛都在向右看齐。对于奶牛 i i i,如果奶牛 j j j 满足 i < j i<j i<j H i < H j H_i<H_j Hi<Hj,我们可以说奶牛 i i i 可以仰望奶牛 j j j。 求出每只奶牛离她最近的仰望对象。

Input

输入格式

  1. * Line 1: A single integer: N

* Lines 2…N+1: Line i+1 contains the single integer: H_i

1 1 1 行输入 N N N,之后每行输入一个身高 H i H_i Hi

输出格式

* 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 N N 行,按顺序每行输出一只奶牛的最近仰望对象,如果没有仰望对象,输出 0 0 0

样例 #1

样例输入 #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 6 6 头奶牛的身高分别为 3,2,6,1,1,2。

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

【数据规模】

对于 20 % 20\% 20% 的数据: 1 ≤ N ≤ 10 1\le N\le10 1N10

对于 50 % 50\% 50% 的数据: 1 ≤ N ≤ 1 0 3 1\le N\le10^3 1N103

对于 100 % 100\% 100% 的数据: 1 ≤ N ≤ 1 0 5 , 1 ≤ H i ≤ 1 0 6 1\le N\le10^5,1\le H_i\le10^6 1N105,1Hi106

分析:

对于本题,主要思路是使用单调栈来解决。

首先,创建3个数组:a[]、b[]和c[]

  • a[]用于记录奶牛的身高。
  • b[]用于记录每头奶牛所仰望的奶牛的下标。
  • c[]是一个单调栈,用于记录奶牛的下标。

单调栈:使用了单调栈的思想来找到每头奶牛所仰望的对象。

  • 从后往前遍历奶牛,保证单调栈中的元素单调递减,这样可以确保栈顶奶牛身高大于当前奶牛。

  • 如果当前奶牛的身高大于等于栈顶奶牛的身高,说明栈顶奶牛不再是当前奶牛的仰望对象,需要出栈,直到栈顶奶牛的身高大于当前奶牛的身高或者栈为空。

  • 然后将当前奶牛的下标存入栈中。

最后,遍历数组b,输出每头奶牛仰望的下标。

这段代码的时间复杂度为O(n),其中n为奶牛的数量。因为使用了单调栈,使得在一次遍历中就能够得到每头奶牛仰望的下标,而不需要额外的操作。

代码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int [] a = new int [n+10]; // 记录奶牛身高
        int [] b = new int [n+10]; // 记录奶牛仰望的下标
        int [] c = new int [n+10];// 单调栈,记录奶牛下标
        for(int i = 1;i <= n;i++) a[i]= sc.nextInt();
        
        int tt = 0;// tt 表示栈顶
        for(int i = n;i >0;i--) {
        	// 保证单调栈 单调递减
        	while(tt>0&&a[i]>=a[c[tt]]) tt--;
        	b[i] = c[tt];//记录该奶牛的仰望对象
        	c[++tt] = i;//将该奶牛存入栈中
        }
        
        for(int i = 1;i <= n;i++) {
        	System.out.println(b[i]);
        }
    }
}
  • 35
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值