Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 너무 어렵다
- 패스트파이브
- childfragment
- 후기
- 파이썬
- 재밌긴함
- MVVM
- 자바
- 중첩네비게이션
- Android
- 싱글액티비티
- Stack
- 백준
- 알고리즘
- 공유오피스
- fragmentcontainer
- SAA
- 패파
- Kotlin
- rxandroid
- 사무실
- innernavigation
- media3
- 코틀린
- media3 transformer
- 내부프레그먼트
- 안드로이드
- parentfragment
- 스택
- 아키텍쳐
Archives
삽질도사
[백준] 달력 20207 자바 본문
포문 돌릴 때 최대y는 명령갯수이고, 최대x는 스케줄마지막 날짜(제일큰 마지막 수)로 돌렸습니다.
start 기준으로 정렬해서 리스트에 넣어주고 순서대로 배열에 집어넣었고
이후에 연산 엄청나게 까다로웠습니다.
그냥보고하면 이해할 수는 있는데, 짜잘한 실수가 많이 나올 수 있는 로직이었습니다.
전체적으로 포문을 돌리면서 채워진 곳을 만나면 y의 최대값을 갱신하고, 그게 아닌경우는 무시.
모든 y에서 0이라면, if문을 만나서 값을 초기화(비어있는 스케줄)
마지막에 비어있는 칸이 아닐 경우에만 dx++를 해주면 되는 간단한 방법이지만, x값은 계속 증가하나, 비어있는 칸부터 비어있는 칸 까지의 값만을 체크해주어야 하는데 이부분이 생각하기 힘들었습니다.
개인적으로 문제 난이도가 실버1인데 체감은 골3~4정도 였던 문제였습니다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class 달력20207 {
static int[][] map = new int[1002][367];
static int s, e, n, max;
static ArrayList<pair> arr = new ArrayList<>();
static class pair implements Comparable<pair> {
int s, e;
pair(int s, int e) {
this.s = s;
this.e = e;
}
@Override
public int compareTo(pair o) {
if (s == o.s) {
return o.e - e;
} else
return s - o.s;
}
}
static int check(int start, int end) { //몇 칸까지 내려가야하는지 계산하고
int layer = 1;
for (int y = 1; y <= n; y++) {
boolean flag = false;
for (int x = start; x <= end; x++) {
int cur = map[y][x];
if (cur == 1) {
flag = true;
break;
}
}
if (!flag) {
layer = y;
break;
}
}
return layer;
}
static void func(int start, int end) { //계산한 곳까지 채움
int y = check(start, end);
for (int x = start; x <= end; x++) {
map[y][x] = 1;
}
}
static int cal() { // 마지막으로 계산
int sum = 0;
int dy = 0;
int dx = 0;
boolean flag=false;
for (int x = 1; x <= max; x++) {
flag = false;
for (int y = 1; y <= n; y++) {
if (map[y][x] == 1) {
flag = true;
dy = Math.max(dy, y);
}
}
if (!flag) {
sum += dy * dx;
dy = 0;
dx = 0;
} else
dx++;
}
//비어있는 칸이 있는데도 계산하려니까 for문을 빠져나오기
//때문에 마지막 연산은 for문 밖에서 해주어야한다.
if(flag) {
sum+=dy*dx;
}
return sum;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
for (int x = 0; x < n; x++) {
st = new StringTokenizer(br.readLine());
s = Integer.parseInt(st.nextToken());
e = Integer.parseInt(st.nextToken());
max = Math.max(max, e);
arr.add(new pair(s, e));
}
Collections.sort(arr);
for (pair p : arr) {
int start = p.s;
int end = p.e;
func(start, end);
}
System.out.println(cal());
}
}
'백준' 카테고리의 다른 글
[백준] 10799 쇠막대기 자바 (0) | 2021.12.27 |
---|---|
[백준] 빗물 14719 자바 (0) | 2021.03.24 |
[백준] 단어뒤집기2 17413 자바 (0) | 2021.03.16 |
[백준] 배열돌리기1 16926 자바 (0) | 2021.03.12 |
[백준] 행복 유치원 13164 자바 (0) | 2021.03.12 |