前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >atcoder它A Mountaineer

atcoder它A Mountaineer

作者头像
全栈程序员站长
发布2022-07-06 09:30:09
1890
发布2022-07-06 09:30:09
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君。

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.

代码语言:javascript
复制
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

代码语言:javascript
复制
      3
      
      1
      
      2
      
      3

Output Example 1

代码语言:javascript
复制
      0
      
      1
      
      2

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


Input Example 2

代码语言:javascript
复制
      5
      
      1
      
      2
      
      3
      
      2
      
      1

Output Example 2

代码语言:javascript
复制
      0
      
      1
      
      4
      
      1
      
      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

代码语言:javascript
复制
      5
      
      3
      
      2
      
      1
      
      2
      
      3

Output Example 3

代码语言:javascript
复制
      4
      
      2
      
      0
      
      2
      
      4

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


Input Example 4

代码语言:javascript
复制
      8
      
      4
      
      3
      
      2
      
      3
      
      4
      
      3
      
      2
      
      1

Output Example 4

代码语言:javascript
复制
      7
      
      2
      
      0
      
      2
      
      7
      
      2
      
      1
      
      0

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

代码语言:javascript
复制
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/有点像。

代码语言:javascript
复制
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.cn/117392.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月6,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem
  • Input
  • Achievements and Points
  • Output
  • Input Example 1
  • Output Example 1
  • Input Example 2
  • Output Example 2
  • Input Example 3
  • Output Example 3
  • Input Example 4
  • Output Example 4
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档