Skip to content

Commit 7239972

Browse files
authored
고층 건물 (#82)
1 parent e184765 commit 7239972

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

pr/고층 건물/Main.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
public static void main(String[] args) {
6+
new Main().run();
7+
}
8+
public void run() {
9+
FastReader fr = new FastReader();
10+
int n = Integer.parseInt(fr.next());
11+
long[] arr = new long[n];
12+
for(int i=0; i<n; i++) arr[i] = Long.parseLong(fr.next());
13+
this.n = n;
14+
this.arr = arr;
15+
System.out.println(this.solve(n, arr));
16+
}
17+
public int n;
18+
public long[] arr;
19+
public long max = Long.MIN_VALUE;
20+
21+
/**
22+
* 문제 해결 메서드
23+
*
24+
* @param 문제에 맞는 파라미터들
25+
* @return 문제에 맞는 반환 타입
26+
*/
27+
public long solve(int n, long[] arr) {
28+
for (int i = 0; i < n; i++) {
29+
long total = left(i) + right(i);
30+
max = Math.max(max, total);
31+
}
32+
33+
return max;
34+
}
35+
36+
public long right(int cur) {
37+
int count = 0;
38+
double maxSlope = Double.MIN_VALUE;
39+
for (int next = cur + 1; next < n; next++) {
40+
double slope = (double) (arr[next] - arr[cur]) / (next - cur);
41+
if (next == cur + 1 || maxSlope < slope) {
42+
count++;
43+
maxSlope = slope;
44+
}
45+
}
46+
47+
return count;
48+
}
49+
50+
public long left(int cur) {
51+
int count = 0;
52+
double minScope = Double.MAX_VALUE;
53+
for (int prev = cur - 1; prev >= 0; prev--) {
54+
double slope = (double) (arr[prev] - arr[cur]) / (prev - cur);
55+
56+
if (prev == cur - 1 || minScope > slope) {
57+
count++;
58+
minScope = slope;
59+
}
60+
}
61+
return count;
62+
}
63+
64+
static class FastReader {
65+
BufferedReader br; StringTokenizer st;
66+
public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); }
67+
public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } } return st.nextToken(); }
68+
public int nextInt() { return Integer.parseInt(next()); }
69+
public long nextLong() { return Long.parseLong(next()); }
70+
public boolean hasMoreTokens() { while (st == null || !st.hasMoreElements()) { String line = null; try { line = br.readLine(); } catch (IOException e) { } if (line == null) return false; st = new StringTokenizer(line); } return true; }
71+
}
72+
}

0 commit comments

Comments
 (0)