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 ...
随机推荐
- Spring中Quartz动态设置cronExpression
字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 或者 JAN- ...
- ExtJS与JQuery对照
首先在介绍ExtJS和JQuery,然后进行比较 一个.什么是ExtJS? 1.ExtJS能够用来开发RIA也即富client的AJAX应用,是一个用javascript写的,主要用于创建前端用户界面 ...
- ZOJ1093 动态规划
给你n砖,有三个长宽高.每一个无限制的访问.叠加在一个条件的长度和宽度必须严格格长度和宽度大于下面的一个,叠加求最大高度. 思维: 每块砖终于放置在根据本方法可以把六种,然后,对于长度和宽度排序.这是 ...
- linux下Ftp环境的搭建
Ftp环境的搭建 1.ftp软件的安装 使用ssh远程连接linux系统,上传和下载一些文件,Ftp是不可少的 Ftp的安装很简单,远程登录系统后使用命令 yum list vsftpd 通过提示 ...
- Outlook将收到邮件的附件保存在磁盘
1. 新建一个宏 1)文件->选项->自定义功能区, 把主选项卡的 开发工具勾选上. 2)开发工具->宏,输入宏名,创建. 加入以下代码 Public Sub SaveAttach( ...
- MVC Razor视图引擎控件
0.日期转化
- Google Protocol Buffers和java字符串处理控制
大多数的操作码被从夜晚复制.懒得敲. 直接在源代码和测试结果如下. serabuffer.proto档.使用下面的命令来生成java代码. protoc -I=./ --java_out=./ ser ...
- HTML<!DOCTYPE> 宣示
在html页面,下面这行代码到底有什么用呢? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...
- hidden change事件
原文:hidden change事件 对于隐藏域hidden无法触发onchange的解决方法:在更改此隐藏域的时候,调用下它的onchange方法,使用jquery的话, 就直接加上 $(" ...
- SqlServer service broker 分布式系统(赵松桃)跳水 2005 数据库编程
1.创建一个测试数据库 create database ServerbrokerTest on primary( name=ServerbrokerTest_data, filename='C:\Pr ...