atcoder它A Mountaineer
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
- 3
Output Example 1
- 0
- 1
- 2
From each hut he can see every huts on the west.
Input Example 2
- 5
- 1
- 2
- 3
- 2
- 1
Output Example 2
- 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
- 5
- 3
- 2
- 1
- 2
- 3
Output Example 3
- 4
- 2
- 0
- 2
- 4
Note that he can see the huts on the equal elevation.
Input Example 4
- 8
- 4
- 3
- 2
- 3
- 4
- 3
- 2
- 1
Output Example 4
- 7
- 2
- 0
- 2
- 7
- 2
- 1
- 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]);
}
}
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
atcoder它A Mountaineer的更多相关文章
- AtCoder Regular Contest 061
AtCoder Regular Contest 061 C.Many Formulas 题意 给长度不超过\(10\)且由\(0\)到\(9\)数字组成的串S. 可以在两数字间放\(+\)号. 求所有 ...
- AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识
链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...
- AtCoder Regular Contest 082
我都出了F了……结果并没有出E……atcoder让我差4分上橙是啥意思啊…… C - Together 题意:把每个数加1或减1或不变求最大众数. #include<cstdio> #in ...
- AtCoder Regular Contest 069 D
D - Menagerie Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement Snuke, w ...
- AtCoder Regular Contest 076
在湖蓝跟衡水大佬们打的第二场atcoder,不知不觉一星期都过去了. 任意门 C - Reconciled? 题意:n只猫,m只狗排队,猫与猫之间,狗与狗之间是不同的,同种动物不能相邻排,问有多少种方 ...
- AtCoder Grand Contest 016
在雅礼和衡水的dalao们打了一场atcoder 然而窝好菜啊…… A - Shrinking 题意:定义一次操作为将长度为n的字符串变成长度n-1的字符串,且变化后第i个字母为变化前第i 或 i+1 ...
- AtCoder Beginner Contest 069【A,水,B,水,C,数学,D,暴力】
A - K-City Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement In K-city, ...
- AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle
https://beta.atcoder.jp/contests/abc075/tasks/abc075_d 题意: 给出坐标平面上n个点的坐标,要求找到一个面积最小的矩形使得这个矩形的边界加上内部的 ...
- AtCoder Beginner Contest 073
D - joisino's travel Time Limit: 2 sec / Memory Limit: 256 MB Score : 400400 points Problem Statemen ...
随机推荐
- 判断 iPhone 是否已插入 SIM 卡的方法
判断 iPhone 是否插入了 SIM 卡,可以参考苹果官网的 systemconfigure framework 教程,将下面的代码复制到头文件 extern NSString* const kCT ...
- windows phone 7 客户端和web的交互(WebBrowser的使用)
原文:windows phone 7 客户端和web的交互(WebBrowser的使用) 前几天看到淘宝的Android客户端,有种促销的功能,当点击促销的时候连接的淘宝促销wap页面,然后点击商品后 ...
- Java整型数组的最大长度到底有多长?
Java整型数组的最大长度到底有多长? 今天上网查了一下,各种说法都有,这个问题似乎总困扰我们Java初学者,无奈,只好自己试了一下,以下是我的测试代码,如果有错误,还望不吝赐教! 使用eclipse ...
- (step7.2.1)hdu 1395(2^x mod n = 1——简单数论)
题目大意:输入一个整数n,输出使2^x mod n = 1成立的最小值K 解题思路:简单数论 1)n可能不能为偶数.因为偶数可不可能模上偶数以后==1. 2)n肯定不可能为1 .因为任何数模上1 == ...
- Ansj配置指南!
=.= 折腾死 ①你想要http://maven.ansj.org/org/ansj/ansj_seg/找一个尽可能高的版本号,比方2.0.7,点进去之后找到相应的jar,比方ansj_seg-2.0 ...
- 6月27日CTO俱乐部下午茶印象
作者:朱金灿 来源:http://blog.csdn.net/clever101 感谢CSDN的邀请,有幸参加了6月27日“CTO俱乐部下午茶时光:CTO在团队管理中所遇到的那些事”活动.本期的主讲嘉 ...
- HDU 1856 More is better(并查集+离散化)
题目地址:HDU 1856 水题.因为标号范围太大,而数据数仅仅有10w,所以要先进行离散化.然后就是裸的并查集了. 代码例如以下: #include <iostream> #includ ...
- WindowsPhone 在 根据公历 获取月球日期数据
WindowsPhone 在 根据公历 获取月球日期数据 WindowsPhone 在 它们的定义 类,根据公历 获取月球日期数据 using System; using System.Collect ...
- oracle查询和编写数据字典
在项目交付时假设须要编写数据字典,能够採用以下的方法.首先执行以下的sql语句 SELECT A.TABLE_NAME AS 表名, A.COLUMN_NAME AS 字段名, DECODE(A.CH ...
- putty中的一些经常使用操作
(和Linux中操作差点儿相同s) 删除文件夹 rm -rf /home/apache-tomcat-8.0.9 就会把home下的apache-tomcat-8.0.9目录给删除了 删除文件 rm ...