atcoder它A Mountaineer

atcoder它A Mountaineer

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB

Problem

Dave is a mountaineer. He is now climbing a range of mountains.

On this mountains, there are N huts located on a straight lining from east to west..

The huts are numbered sequentially from 1 to N. The west most hut is 1, the east most hut is N. The i-th hut is located at an elevation of hi meters.

Dave wants to know how many huts he can look down and see from each hut.

He can see the j-th hut from the i-th hut if all huts between the i-th hut and the j-th hut including the j-th one are located at equal or lower elevation than hi.

Note that the i-th hut itself is not included in the hut he can see from the i-th hut.


Input

The input will be given in the following format from the Standard Input.

N
h1
h2
:
hN
  • On the first line, you will be given N(1≦N≦105), the number of huts.
  • Then N lines follow, each of which contains hi(1≦hi≦105) the elevation of the i-th hut.

Achievements and Points

Your answer will be checked for two levels.

  • When you pass every test case which satisfies 1≦N≦3,000, you will be awarded 30 points.
  • In addition, if you pass all the rest test cases which satisfy 1≦N≦105, you will be awarded 70 more points, summed up to 100points.

Output

On the i-th line, output the number of huts Dave can see from the i-th hut. Make sure to insert a line break at the end of the output.


Input Example 1

     
     
  1. 3
  2. 1
  3. 2
  4. 3

Output Example 1

     
     
  1. 0
  2. 1
  3. 2

From each hut he can see every huts on the west.


Input Example 2

     
     
  1. 5
  2. 1
  3. 2
  4. 3
  5. 2
  6. 1

Output Example 2

     
     
  1. 0
  2. 1
  3. 4
  4. 1
  5. 0

From the 1st and 5th hut he can’t see any other huts.

From the 2nd hut he can only see the 1st hut.

From the 4th hut he can only see the 5th hut.

From the 3rd hut he can see every other huts.


Input Example 3

     
     
  1. 5
  2. 3
  3. 2
  4. 1
  5. 2
  6. 3

Output Example 3

     
     
  1. 4
  2. 2
  3. 0
  4. 2
  5. 4

Note that he can see the huts on the equal elevation.


Input Example 4

     
     
  1. 8
  2. 4
  3. 3
  4. 2
  5. 3
  6. 4
  7. 3
  8. 2
  9. 1

Output Example 4

     
     
  1. 7
  2. 2
  3. 0
  4. 2
  5. 7
  6. 2
  7. 1
  8. 0

思路:本题是个简单题,我却一直面对大数据时TLE。直接上TLE源代码

import java.util.Scanner;
 
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int count = sc.nextInt();
		int num[] = new int[count];
		int flag[] = new int[count];
		for (int i = 0; i < count; i++) {
			num[i] = sc.nextInt();
		}
		for (int i = 0; i < count; i++) {
			for (int j = i - 1; j >= 0 && num[i] >= num[j]; j--,flag[i]++);
			for (int j = i + 1; j < count && num[i] >= num[j]; j++,flag[i]++);
			System.out.println(flag[i]);
 
		}
	}
}

以下是AC源代码。依据上面的源代码做了一定程度上的优化。从某种程度上来说,更改了部分思路,和
https://oj.leetcode.com/problems/candy/有点像。

import java.util.Scanner;public class Main {	public static void main(String[] args) {		Scanner sc = new Scanner(System.in);		int count = sc.nextInt();		int num[] = new int[count];		int[] back = new int[count];		int[] forward = new int[count];		for (int i = 0; i < count; i++) {			num[i] = sc.nextInt();		}		for (int i = 0; i < count; i++) {			for (int j = i - 1; j >= 0 && num[i] >= num[j]; 					back[i] = back[i]+ back[j] + 1, j = j - back[j] - 1);		}		for (int i = count - 1; i >= 0; i--) {			for (int j = i + 1; j < count && num[i] >= num[j];					forward[i] = forward[i]+ forward[j] + 1, j = j + forward[j] + 1);		}		for (int i = 0; i < count; i++) {			System.out.println(back[i] + forward[i]);		}	}}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117392.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月6日 下午2:00
下一篇 2022年1月6日 下午2:00


相关推荐

  • 2019工程伦理慕课答案(2019秋)习题及期末答案

    2019工程伦理慕课答案(2019秋)习题及期末答案第一章习题(下)单选题(1/1point)下列哪一项不是工程与技术的区别内容和性质目的活动主体任务、对象和思维方式单选题(1/1point)下列哪一项不是工程活动的特征自主性创造性社会性确定性多选题(1points)下列哪项是工程的完整生命周期中的环节计划设计评估完成判断题(1/1point)计划、设计、建造…

    2022年5月30日
    41
  • Java编程——九九乘法表

    Java编程——九九乘法表使用Java语言实现九九乘法表的输出,这里利用的是for循环实现输出九九乘法表。最后输出使用的是print而不是println,注意两者的区别。简单来说,就是println在输出一个语句结束之后会自动换行,而print在输出一个语句结束之后不会自动换行。可根据代码的不同做出合适的调整,实现不同的输出结果。publicclassdemo7{publicstaticvoidmain(String[]args){for(inti=1;i<=9;i+

    2022年7月15日
    21
  • Nano Banana 新手教程:小红书最火AI手办提示词与实用技巧全揭秘

    Nano Banana 新手教程:小红书最火AI手办提示词与实用技巧全揭秘

    2026年3月13日
    4
  • pycharm如何创建py文件_pycharm输入不了

    pycharm如何创建py文件_pycharm输入不了PyCharm是一款很好用的编写Python工程的IDE,用PyCharm创建一个Python文件或者向工程添加一个.py文件时,为了更好的使所编写的代码在各操作环境更好的运行,我们往往需要在.py文件中添加头文件标注相关信息。例如:打开PyCharm程序,根据菜单栏中按照如下进入设置:File->settings->Editor->FileandCodeTem…

    2022年8月26日
    11
  • Spring Boot 入门教程

    Spring Boot 入门教程SpringBoot说是一全新框架,但实质上还是我们的Spring。只是它帮我们做了那些SpringBean配置,比如那堆恶心的xml。它使用“习惯优于配置”,就是默认给你配置了项目构建时都需要的配置,并且内嵌了tomcat,让你基本不用写配置文件就能轻松搭建一个项目。这里我用的是Idea2017和java8(理论上java6以上就可以)1.0 用SpringInitializr

    2022年7月15日
    22
  • JAVA零基础入门教程 第一节 基本的Dos命令

    JAVA零基础入门教程 第一节 基本的Dos命令

    2026年3月15日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号