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


相关推荐

  • MGN网络详解以及代码分析「建议收藏」

    MGN网络详解以及代码分析「建议收藏」MGN网络详解以及代码分析最近阅读了云从科技最新的关于REID的论文以及相关的博客和代码,算法是基于MGN,关于网络的部分,这里记录一些自己的学习笔记。以下是我参考的博客和代码的网址博客:https://blog.csdn.net/Gavinmiaoc/article/details/80840193代码:https://github.com/Gavin666Github/reid-m…

    2022年10月6日
    3
  • Map转json遇到一些问题

    Map转json遇到一些问题最近发现了一个问题,通过查看用户的活跃度发现了奇怪的事情,有的用户访问某一个接口没有问题,而一些奇葩用户访问这一接口就是不成功,经过查看,原来是Android系统4.4以下map转换json的时候出现了问题,具体是什么了,下面我们来分析分析。第一,利用”org.json.JSONObject”下的JsonObject时,4.4以下的系统出现“=”的问题。比如:Map

    2022年6月20日
    34
  • Python爬虫—-网页下载器和urllib2模块及对应的实例

    Python爬虫—-网页下载器和urllib2模块及对应的实例网页下载器:将互联网上URL对应的网页下载到本地的工具,是爬虫的核心组件未完。。。

    2022年5月8日
    39
  • Virtualbox下使用virt-p2v

    Virtualbox下使用virt-p2v1虚拟机迁移参考:http://www.ibm.com/developerworks/cn/linux/l-cn-mgrtvm1/index.html2物理机到虚拟机的迁移virt-p2vredhat官方文档:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/V2V_Guide/cha

    2022年7月26日
    11
  • Computer Science 学习第四章–CPU 指令集和指令处理

    Computer Science 学习第四章–CPU 指令集和指令处理

    2022年1月9日
    51
  • ubuntu安装pycharm快捷图标_pycharm快捷方式找不到了

    ubuntu安装pycharm快捷图标_pycharm快捷方式找不到了1、首先下载pycharm安装包,从官网下载,选择专业版。2、解压到一个文件夹,打开bin文件夹,命令行下运行pycharm.sh文件。sh./pycharm.sh3、然后出现安装过程,一步一步走下去就行,如果中间问是否需要加载以前的设置(如果以前安装过),可以加也可以不加。4、激活码选择企业版,可以输入:http://idea.imsxm.com/5、完成安装。但是这样每次打开pycharm,需

    2022年8月26日
    6

发表回复

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

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