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.

约翰的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

输出格式:

  • 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
输出样例#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.

分析:和前几题差不多,不过就是要记录一下每次栈顶的位置.

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int n,h[],ans[],stk[],top,num[]; int main()
{
scanf("%d", &n);
for (int i = ; i <= n; i++)
scanf("%d", &h[i]); for (int i = n; i >= ; i--)
{
while (top != && stk[top] <= h[i])
top--;
ans[i] = num[top];
stk[++top] = h[i];
num[top] = i;
}
for (int i = ; i <= n; i++)
printf("%d\n", ans[i]); return ;
}

洛谷P2947 [USACO09MAR]仰望Look Up的更多相关文章

  1. 洛谷 P2947 [USACO09MAR]仰望Look Up

    题目描述 Farmer John's N (1 <= N <= 100,000) cows, conveniently numbered 1..N, are once again stan ...

  2. 洛谷 P2947 [USACO09MAR]向右看齐Look Up【单调栈】

    题目描述 Farmer John's N (1 <= N <= 100,000) cows, conveniently numbered 1..N, are once again stan ...

  3. 洛谷 P2947 [USACO09MAR]向右看齐Look Up

    目录 题目 思路 \(Code\) 题目 戳 思路 单调栈裸题 \(Code\) #include<stack> #include<cstdio> #include<st ...

  4. 洛谷P2947 [USACO09MAR]向右看齐Look Up

    #include<cstdio> #include<algorithm> #include<stack> #include<cctype> using ...

  5. 【洛谷P2947】向右看齐

    向右看齐 题目链接 此题可用单调栈O(n)求解 维护一个单调递减栈,元素从左到右入栈 若新加元素大于栈中元素,则栈中元素的仰望对象即为新加元素 每次将小于新加元素的栈中元素弹出,记录下答案 #incl ...

  6. 洛谷 P2945 [USACO09MAR]沙堡Sand Castle 题解

    题目传送门 大概思路就是把这两个数组排序.在扫描一次,判断大小,累加ans. #include<bits/stdc++.h> using namespace std; int x,y,z; ...

  7. 洛谷2944 [USACO09MAR]地震损失2Earthquake Damage 2

    https://www.luogu.org/problem/show?pid=2944 题目描述 Wisconsin has had an earthquake that has struck Far ...

  8. 洛谷 P2945 [USACO09MAR]沙堡Sand Castle

    传送门 题目大意: ai,ai+1,ai+2... 变成 bi,bi+1,bi+2.. 不计顺序,增加和减少a数组均有代价. 题解:贪心+排序 小的对应小的 代码: #include<iostr ...

  9. 洛谷2943 [USACO09MAR]清理Cleaning Up——转变枚举内容的dp

    题目:https://www.luogu.org/problemnew/show/P2943 一下想到n^2.然后不会了. 看过TJ之后似乎有了新的认识. n^2的冗余部分在于当后面那部分的种类数一样 ...

随机推荐

  1. js实现指定日期增加指定月份

    首先,大致思路为: 1. 先将字符串格式的时间类型转化为Date类型 2. 再将Date类型的时间增加指定月份 3. 最后将Date类型的时间在转化为字符串类型 1.  先将字符串格式的时间类型转化为 ...

  2. red hat的防火墙怎么关闭

    查看是否开启: service iptables status 关闭方法: service iptables stop 永远关闭: Ntsysv 把iptables前的*号去掉. 查看SELinux状 ...

  3. java基础编程——链表反转

    题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList.   题目代码 /** * @program: JavaCode * @description:输入一个链表,按链表值从尾到头 ...

  4. vue2.0父子组件以及非父子组件通信

    官网API: https://cn.vuejs.org/v2/guide/components.html#Prop 一.父子组件通信 1.父组件传递数据给子组件,使用props属性来实现 传递普通字符 ...

  5. 井字游戏 人机对战 java实现

    package com.ecnu.Main; /** * 主函数触发游戏 */public class MainApplication { public static void main(String ...

  6. 【Django】Django中datetime的处理(strftime/strptime)

    strftime<将date,datetime,timezone.now()类型处理转化为字符串类型> strftime()函数是用来格式化一个日期.日期时间和时间的函数,支持date.d ...

  7. shell脚本中case的用法

    shell脚本中case选择语句可以结合read指令实现比较好的交互应答操作,case接收到read指令传入的一个或多个参数,然后case根据参数做选择操作. case的语法如下 case $char ...

  8. Linux常用关机重启命令

    # shutdown -h 10       //计算机将在10分钟后 关机,且会显示在登录用户的当前屏幕中 # shutdown -h now    //立即 关机 # shutdown -h 20 ...

  9. 项目10.2-企业级自动化运维工具---puppet详解

    1.认识puppet 1.1 引入 puppet是什么,咱们先不用专业的名词解释它,咱们先描述一些工作场景,看明白这些工作场景,自然会知道puppet是什么. (1)场景一: 管理员想要在100台服务 ...

  10. JZOJ 3487. 【NOIP2013模拟联考11】剑与魔法(dragons)

    3487. [NOIP2013模拟联考11]剑与魔法(dragons) (Standard IO) Time Limits: 1000 ms  Memory Limits: 131072 KB  De ...