-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from HYU-PS-STUDY/clean2001
[week-12] 1027, 1406
- Loading branch information
Showing
4 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
// 1027. 고층 건물 | ||
class Main { | ||
static int N; | ||
static int[] arr; | ||
|
||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
N = Integer.parseInt(br.readLine()); | ||
arr = new int[N]; | ||
|
||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
for(int i=0; i<N; ++i) { | ||
arr[i] = Integer.parseInt(st.nextToken()); | ||
} | ||
|
||
int ans = 0; | ||
for(int b=0; b<N; ++b) { // b번째 건물을 기준으로 양옆에 건물 몇개 보이는지 확인 | ||
// 뒤쪽 확인 | ||
int cnt1 = 0; | ||
for(int i=b-1; i>=0; i--) { // i와 b 건물 사이 | ||
// 직선을 구하기 | ||
double t = tan(i, b, arr[i], arr[b]); | ||
double k = y_meet(i, arr[i], t); | ||
|
||
// i~b 사이에 건물들이 닿는지를 확인 | ||
boolean flag = false; | ||
for(int j=i+1; j<b; ++j) { | ||
double y_hat = t * (double)j + k; // | ||
if(y_hat <= (double) arr[j]) { | ||
flag = true; | ||
break; | ||
} | ||
} | ||
|
||
if(!flag) { // 안 닿았음 -> 됨 | ||
cnt1++; | ||
} | ||
|
||
} | ||
|
||
// 앞쪽 확인 | ||
int cnt2 = 0; | ||
for(int i=b+1; i<N; i++) { // b와 i 사이 | ||
// 직선을 구하기 | ||
double t = tan(i, b, arr[i], arr[b]); | ||
double k = y_meet(i, arr[i], t); | ||
|
||
// b~i 사이에 건물들이 닿는지를 확인 | ||
boolean flag = false; | ||
for(int j=b+1; j<i; ++j) { | ||
double y_hat = t * (double)j + k; // | ||
if(y_hat <= (double) arr[j]) { | ||
flag = true; | ||
break; | ||
} | ||
} | ||
|
||
if(!flag) { // 안 닿았음 -> 됨 | ||
cnt2++; | ||
} | ||
|
||
} | ||
|
||
|
||
if(ans < cnt1+cnt2) { | ||
ans = cnt1+cnt2; | ||
|
||
} | ||
} | ||
|
||
System.out.println(ans); | ||
|
||
} | ||
|
||
public static double tan(int x1, int x2, int y1, int y2) { | ||
if(x1 == x2) return 0; // 있을 수 없는 경우 | ||
|
||
return (((double)y2-(double)y1) / ((double)x2-(double)x1)); | ||
} | ||
|
||
public static double y_meet(int x1, int y1, double t) { | ||
return (double)y1 - t * (double)x1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import java.io.*; | ||
import java.util.*; | ||
// 1406. 에디터 | ||
// 스택을 쓰지 않고, 직접 문자열을 고치는 식으로 구현하면 | ||
// substring()과 같은 O(N)인 메소드 때문에 시간초과가 발생합니다. | ||
class Main { | ||
|
||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
String input = br.readLine(); | ||
|
||
int N = Integer.parseInt(br.readLine()); | ||
Deque<String> st1 = new LinkedList<>(); | ||
Deque<String> st2 = new LinkedList<>(); | ||
|
||
for(int i=0; i<input.length(); ++i) { | ||
st1.addLast(Character.toString(input.charAt(i))); | ||
} | ||
for(int i=0; i<N; ++i) { | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
|
||
String cmd = st.nextToken(); | ||
|
||
if(cmd.equals("P")) { // 추가하기 -> st1에 추가 | ||
String ch = st.nextToken(); | ||
st1.addLast(ch); | ||
} else if(cmd.equals("L")) { // 왼쪽으로 이동 -> s1에서 s2로 옮기기 | ||
if(st1.isEmpty()) continue; | ||
st2.addLast(st1.pollLast()); | ||
} else if(cmd.equals("B")) { // 삭제 -> s1하나 삭제 | ||
if(st1.isEmpty()) continue; | ||
st1.removeLast(); | ||
} else if(cmd.equals("D")) { // 오른쪽으로이동 | ||
if(st2.isEmpty()) continue; | ||
st1.addLast(st2.pollLast()); | ||
} | ||
|
||
} | ||
|
||
|
||
StringBuilder sb1 = new StringBuilder(); | ||
while(!st1.isEmpty()) { | ||
sb1.append(st1.pollLast()); | ||
} | ||
StringBuilder sb2 = new StringBuilder(); | ||
while(!st2.isEmpty()) { | ||
sb2.append(st2.pollLast()); | ||
} | ||
|
||
|
||
System.out.println(sb1.reverse().toString() + sb2.toString()); | ||
|
||
} | ||
} |