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

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

时间: 2024-10-02 10:16:50

atcoder之A Mountaineer的相关文章

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 sequenti

AtCoder Regular Contest 075 E - Meaningful Mean 树状数组求顺序对, 前缀和

题目链接: http://arc075.contest.atcoder.jp/tasks/arc075_c 题意: 给你一个序列和一个数k,求有多少对l,r,使得a[l]+a[l+1]+...+a[r]的算术平均数大于等于k 1≤N≤2×10^5 1≤K≤10^9 1≤ai≤10^9 思路: 首先对于所有数减去k,这样就不用除(r-l+1), 然后我们发现所求的就是有多少对l,r,使得sum[r]-sum[l-1] >= 0, sum是减去k之后的序列的前缀和 用树状数组对sum求有多少个顺序对

AtCoder 2346 No Need

传送门:http://arc070.contest.atcoder.jp/tasks/arc070_b?lang=en 题意: 对于一个数组的任意一个子集,如果它的元素和大于等于K这个子集就叫做good subset,如果将一个数所在的所有good subset都减去这个数,这些集合依旧是good subset那么这个数被称为无用数.现在给定一个数组,求其中的无用数的个数.  题解: 如果一个数不是无用数,那么所有大于等于这个数的数都不会是无用数,那么我们先对这个数组进行排序,每次二分结果就行了

A Mountaineer 最详细的解题报告

题目来源:A Mountaineer (不知道该链接是否可以直接访问,所以将题目复制下来了) 题目如下: D - 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

Atcoder Yet Another Palindrome Partitioning(状压dp)

Atcoder Yet Another Palindrome Partitioning 思路: 一个字符串满足条件的情况是奇数字母个数小于等于1,也就是异或起来是1<<j(0<=j<=25) 记mark是异或起来的值 状态转移: dp[mark]=dp[mark]+1; dp[mark]=min(dp[mark^(1<<j)]+1,dp[mark]);(0<=j<=25) 注意dp[0]被转移后可能会变成1,但是由它转移的需要dp[0]=0,所以每次记得把d

2018.03.04 晚上Atcoder比赛

C - March Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement There are N people. The name of the i-th person is Si. We would like to choose three people so that the following conditions are met: The name of every chosen per

Atcoder 091/092 C 2D Plane 2N Points[扫描线]

昨晚打了第一场atcoder...这题卡了1h...今天听课的时候听到了一个极相似的题... 题意:给出2n个点的坐标(x,y) 前n个是红点,剩下是蓝点,当一个红点的横纵坐标都小于一个蓝点的时候,他们可以匹配,求最大的匹配对数 按照横坐标排序,排序后从右往左扫描,发现蓝点将其纵坐标存入set中(因为已经按照横坐标排序,所以不需要考虑横坐标),发现红点从set中找一个能跟这个点匹配的最小的点(lower_bound),注意set::lower_bound是O(logn)的,std::lower_

【赛时总结】 ◇赛时&#183;I◇ AtCoder ARC-098

◆赛时I◆ ARC-098 ■试题&解析■ ◆本场最水◆ C-Attention 长点儿信心吧-- [AtCoder ARC-098 C] [解析] 既然只存在左右(东西)两个朝向,那么领导右侧的人应朝左,相反左侧的人朝右.则要找到一个人作为领导使上述人的数量最多. 我们可以用 tot[0].tot[1] 分别储存最初朝东.西的人的个数.然后从第一个人开始枚举,统计当前人左侧的分别朝向东.西的人的个数,从而算出当前人左侧朝右.右侧朝左的人的个数,记为F.领导人即是算出F最大的人. 注意统计时要排

【赛时总结】 ◇赛时&#183;II◇ AtCoder ABC-100

◆赛时·II◆ ABC-100 ■唠叨■ ABC终于超过百场比赛啦(毫不犹豫地参加).然后莫名其妙的好像是人很多,评测慢得不可理喻.然后我就--交了一大发--错误程序--然后B题就没了.最后的D题居然是贪心(题意没看懂),我就做了个DP还wa了 (:′⌒`) ■试题&解析■ ◆泼水节◆ A-Happy Birthday! [Atcoder ABC-100 A] [翻译] E869120和square1001的16岁生日到了,来自Atcoder王国的Takahashi送给他们一个被平均切成16份的