P2947 [USACO09MAR]仰望Look Up

• 74通过
• 122提交
• 题目提供者洛谷OnlineJudge
• 标签USACO2009云端
• 难度普及/提高-
• 时空限制1s / 128MB

讨论  题解

• 中文翻译应当为向右看齐
• 题目中文版范围。。

题目描述

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.

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>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int n,h[100010],ans[100010],stk[100010],top,num[100010];

int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &h[i]);

for (int i = n; i >= 1; i--)
{
while (top != 0 && stk[top] <= h[i])
top--;
ans[i] = num[top];
stk[++top] = h[i];
num[top] = i;
}
for (int i = 1; i <= n; i++)
printf("%d\n", ans[i]);

return 0;
}```

(0)
(0)

0条