[USACO 2009 Mar S] Look Up

来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

Farmer John’s N (1 <= N <= 100,000) cows, conveniently numbered 1…N, are once again standing in a row. Cow i has height Hi (1 <= Hi <= 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 Hi < Hj. 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.

输入描述:

  • Line 1: A single integer: N
  • Lines 2…N+1: Line i+1 contains the single integer: 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.
  • 示例1
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.


WA:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 1010
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    queue<int> q;
    int arr[maxn];
    cin >> n;
    for (int i = 0; i < n;i++)
    {
        cin >> arr[i];
    }
    q.push(arr[0]);
    for (int i = 0; i < n - 1;i++)
    {
        if(arr[i] > arr[i + 1])
        {
            q.push(arr[i]);
        }
        if(arr[i] == arr[i + 1])
        {
                q.push(arr[i - 1]);
        }
        if(arr[i] < arr[i + 1])
        {
            q.push(0);
        }
    }
    while(!q.empty())
    {
        cout << q.front() << endl;
        q.pop();
    }
    return 0;
}
  • After reading the questions hastily, I will directly look at the test cases!
    WA

AC:

[1]

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
int a[100001], s[100001], res[100001], top;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = n; i >= 0; i--)
    {
        while (top && a[s[top]] <= a[i])
            top--;
        res[i] = s[top];
        s[++top] = i;
    }
    for (int i = 1; i <= n; i++)
        cout << res[i] << endl;
    return 0;
}

[2]

#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
int a[100001], s[100001], top;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    stack<int> q;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = n; i >= 0; i--)
    {
        while (top && a[s[top]] <= a[i])
            top--;
        q.push(s[top]);
        s[++top] = i;
    }
    q.pop();
    while(!q.empty())
    {
        cout << q.top() << endl;
        q.pop();
    }
    return 0;
}

This problem is not very difficult, but when using the STL library in C++,
you need to pay attention to the elements of the stack header!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值