当前位置:网站首页>Atcoder a mountaineer
Atcoder a mountaineer
2022-07-06 18:26:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
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
3
1
2
3Output Example 1
0
1
2From each hut he can see every huts on the west.
Input Example 2
5
1
2
3
2
1Output Example 2
0
1
4
1
0From 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
5
3
2
1
2
3Output Example 3
4
2
0
2
4Note that he can see the huts on the equal elevation.
Input Example 4
8
4
3
2
3
4
3
2
1Output Example 4
7
2
0
2
7
2
1
0Ideas : This is a simple question , But I have been facing big data TLE. Go straight up TLE Source code
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]);
}
}
}Here are AC Source code . According to the above source code, it has been optimized to a certain extent . In a way , Changed some ideas , and https://oj.leetcode.com/problems/candy/ It's kind of like .
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]); } }}Copyright notice : This article is an original blog article . Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117392.html Link to the original text :https://javaforall.cn
边栏推荐
- Common - magic number 7
- 随着MapReduce job实现去加重,多种输出文件夹
- SQL优化问题的简述
- Self-supervised Heterogeneous Graph Neural Network with Co-contrastive Learning 论文阅读
- C language exchanges two numbers through pointers
- The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
- F200 - UAV equipped with domestic open source flight control system based on Model Design
- POJ 2208 已知边四面体六个长度,计算体积
- C语言自动预订飞机票问题
- Easy to use PDF to SVG program
猜你喜欢

MySQL查询请求的执行过程——底层原理

UDP协议:因性善而简单,难免碰到“城会玩”

第三季百度网盘AI大赛盛夏来袭,寻找热爱AI的你!

UDP protocol: simple because of good nature, it is inevitable to encounter "city can play"

【LeetCode第 300 场周赛】

关于这次通信故障,我想多说几句…

Grafana 9.0 正式发布!堪称最强!

Comparative examples of C language pointers *p++, * (p++), * ++p, * (++p), (*p) + +, +(*p)

F200 - UAV equipped with domestic open source flight control system based on Model Design

IP, subnet mask, gateway, default gateway
随机推荐
Implementation of queue
UFIDA OA vulnerability learning - ncfindweb directory traversal vulnerability
使用cpolar建立一个商业网站(1)
D binding function
This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer
d绑定函数
Splay
Excel usage record
Kill -9 system call used by PID to kill process
1700C - Helping the Nature
Penetration test information collection - CDN bypass
[swoole series 2.1] run the swoole first
The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
Cocos2d Lua 越来越小样本 内存游戏
Jerry's access to additional information on the dial [article]
JDBC驱动器、C3P0、Druid和JDBCTemplate相关依赖jar包
STM32+HC05串口蓝牙设计简易的蓝牙音箱
Jerry's watch reads the file through the file name [chapter]
SQL优化问题的简述
随着MapReduce job实现去加重,多种输出文件夹