首页 技术 正文
技术 2022年11月7日
0 收藏 418 点赞 500 浏览 2571 个字

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]);
}
}
}

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849