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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • _bz2 缺少

    _bz2 缺少报错信息from_bz2importBZ2Compressor,BZ2DecompressorModuleNotFoundError:Nomodulenamed’_bz2’解决办法1、安装yuminstallbzip2-devel2、找到_bz2.cpython-37m-x86_64-linux-gnu.so文件如果在机器上没有的话,…

    2022年5月12日
    120
  • 【Android】Broadcasts详解

    【Android】Broadcasts详解Android应用程序可以发送广播,也可以接收Android系统或者其它应用发出的广播,这跟发布-订阅设计模式很相似。当一些受到关心的事件发生后,广播会被自动发送。举例来说,当一些系统事件(如开机,设备开始充电等)发生,Android系统会发送广播。应用程序也可以发送自定义的广播,比如当某个应用关注的事件(如数据更新等)发生后可以发送广播提醒它。系统广播当一系列系统事件发生的时候,系统会自动发送广播

    2022年6月15日
    24
  • pytest指定用例_python测试用例

    pytest指定用例_python测试用例前言测试用例在设计的时候,我们一般要求不要有先后顺序,用例是可以打乱了执行的,这样才能达到测试的效果.有些同学在写用例的时候,用例写了先后顺序,有先后顺序后,后面还会有新的问题(如:上个用例返回

    2022年7月31日
    11
  • 经常使用哈希函数的比較及其C语言实现「建议收藏」

    经常使用哈希函数的比較及其C语言实现

    2022年2月6日
    34
  • pycharm如何执行高级撤销操作回到历史[通俗易懂]

    pycharm如何执行高级撤销操作回到历史[通俗易懂]今天写代码兴奋过头了,认为别人写得太麻烦,所以在看了这个人是要达成什么样的目标之后,把他的代码直接删了,然后自己重写,到后来发现有这样那样的问题,这个时候想参考原来的代码,可是为时已晚,已经是6,7个小时之前了,姑且不问能否一直使用低级撤销ctrl+z,就算可以,估计也要半个小时才能回到6,7个小时之前吧。这个时候,我悲从中来,悔恨自己在最开始的时候没有弄一个备份。但是,痛定思痛,发现了这一个撤销的高级操作,回退到历史,我以前在使用AndroidStudio的时候也有这个功能,所以试了试pycharm

    2022年8月26日
    3
  • idea2021.7.20激活码获取【2021.7最新】

    (idea2021.7.20激活码获取)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月20日
    34

发表回复

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

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