1027번: 고층 건물
세준시에는 고층 빌딩이 많다. 세준시의 서민 김지민은 가장 많은 고층 빌딩이 보이는 고층 빌딩을 찾으려고 한다. 빌딩은 총 N개가 있는데, 빌딩은 선분으로 나타낸다. i번째 빌딩 (1부터 시작)
www.acmicpc.net
- 생각
기울기를 통해서 볼 수 있는 건물의 수를 파악한다. 가장 많이 보이는 건물의 최대값을 유지시켜줌.
애먹었던 부분은 자료형 때문이였는데, angle의 값을 99999999999로 주니까 long 범위를 벗어나버려서 되지 않았다. TIP) 이때 숫자 맨뒤에 l or L을 붙여주면 가능해짐
- 코드
import java.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
static int answer, number;
static long[] array;
public static void main(String[] args) throws Exception {
SetData();
Calculate();
System.out.println(answer);
}
private static void SetData() throws Exception {
InputReader in = new InputReader(System.in);
number = in.nextInt();
array = new long[number+1];
answer = 0;
for(int index = 1; index <= number; index++)
array[index] = in.nextLong();
}
private static void Calculate() {
for(int i = 1;i <= number;i++){
int temp=0;
double angle = 99999999999L;
for(int j = i-1;j >= 1;j--){
double lean=(double)(array[j]-array[i])/(double)(j-i);
if(lean<angle){
temp++;
angle=lean;
}
}
angle = -99999999999L;
for(int j = i+1;j <= number;j++){
double lean=(double)(array[j]-array[i])/(double)(j-i);
if(lean>angle){
temp++;
angle=lean;
}
}
answer=Math.max(answer,temp);
}
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
'algorithm' 카테고리의 다른 글
[BOJ/JAVA] 백준 4963번 : 섬의 개수 (0) | 2021.02.24 |
---|---|
[BOJ/JAVA] 백준 5557번 : 1학년 (0) | 2021.02.23 |
[BOJ/JAVA] 백준 1012번 : 유기농 배추 (0) | 2021.02.19 |
[BOJ/JAVA] 백준 2579번 : 계단 오르기 (0) | 2021.02.18 |
[BOJ/JAVA] 백준 17141번 : 연구소 2 (0) | 2021.02.17 |