백준
[백준] 달력 20207 자바
삽질도사
2021. 3. 23. 20:45
반응형
포문 돌릴 때 최대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());
}
}
반응형