zoukankan      html  css  js  c++  java
  • P2947 [USACO09MAR]向右看齐Look Up

    题目描述

    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

    第 1 行输入 N,之后每行输入一个身高 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.

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

    【输入说明】6 头奶牛的身高分别为 3, 2, 6, 1, 1, 2.

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

    【数据规模】

    对于 20%的数据: 1≤N≤10;

    对于 50%的数据: 1≤N≤1,000;

    对于 100%的数据:1≤N≤100,000;1≤H_i≤1,000,000;

    讲真,,有一种快被果蝇咬死的感觉。。

    这个时候我的同桌小常同志(学生物的),

    就要站出来说了:“你放心!果蝇不吃人的!”,

    那完美的表情,完美的肢体语言。

    我只想说:

    “世界欠你一个奥斯卡!”。

    ,,为什么最近老想一些很血腥的事儿。。。

    靠,我想看动物世界!我想看一千零一夜!我想看跑男!

    下面真的要切入正题了同志们。

    刚开始写了个very beautiful的模拟,

    如下:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<cstring>
     6 using namespace std;
     7 
     8 int n;
     9 int a[100002];
    10 bool flag;
    11 
    12 int main()
    13 {
    14     scanf("%d",&n);
    15     for(int i=1;i<=n;++i)
    16         scanf("%d",&a[i]);
    17     for(int i=1;i<n;++i)
    18     {
    19         flag=0;
    20         for(int j=i+1;j<=n;++j)
    21         {
    22             if(a[j]>a[i]) 
    23             {
    24                 printf("%d
    ",j);
    25                 flag=1;
    26                 break;
    27             }
    28         }
    29         if(flag==0)
    30             printf("0
    ");
    31     }
    32     printf("0");
    33     return 0;
    34 }

    哦,老天!

    为什么只有58分。。。

    好吧,虽然我也不知道错在哪里。

    那就看正解吧!

    wow,正解好漂亮啊。

    那,

    自行理解吧2333.

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<cstring>
     6 using namespace std;
     7 
     8 int n;
     9 int a[100002],pos[100002];
    10 
    11 int main()
    12 {
    13     scanf("%d",&n);
    14     pos[0]=2333333;
    15     for(int i=1;i<=n;++i)
    16         scanf("%d",&a[i]);
    17     for(int i=n-1;i>=1;--i)
    18     {
    19         int j=i+1;
    20         while(a[i]>=a[j]&&a[j]>0)
    21             j=pos[j];
    22         pos[i]=j;
    23     }        
    24     for(int i=1;i<=n;++i)
    25         printf("%d
    ",pos[i]);
    26     return 0;
    27 }

    如果你不开心,那我就把右边这个帅傻子分享给你吧,
    你看,他这么好看,跟个zz一样看着你,你还伤心吗?
    真的!这照片盯上他五秒钟就想笑了。
    一切都会过去的。
    时间时间会给你答案2333
  • 相关阅读:
    Jmeter使用实践-接口diff测试
    Jmeter发送HTTPS请求
    Jmeter使用实践- 基础介绍
    Jmeter性能测试工具的使用(Web性能测试)
    使用JMeter创建FTP测试计划
    Jmeter引用cookies进行登录实战
    Jmeter: PATCH方法无法发送参数的暂时解决方法
    Springboot整合Freemarker
    Springboot整合Jsp
    Springboot文件上传
  • 原文地址:https://www.cnblogs.com/Mary-Sue/p/9260757.html
Copyright © 2011-2022 走看看