티스토리 뷰
14501번: 퇴사
첫째 줄에 백준이가 얻을 수 있는 최대 이익을 출력한다.
www.acmicpc.net
|
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class Main {
static int N, max=0;
static boolean[] sch, select;
static ArrayList<counsel> arr = new ArrayList<>();
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
sch = new boolean[N + 1]; // 스케줄 잡혀있는 날인지? 0 dummy
select = new boolean[N+1]; // 잡힌 상담인지? (pay계산 위함) 0 dummy
arr.add(new counsel(0, 0)); // dummy
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
arr.add(new counsel(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())));
}
// 가능한 경우 탐색
search(1);
System.out.println(max);
}
// 가능한 경우의 수
static void search(int srcIdx) {
if (srcIdx > N) {
// 소득 max 비교
max = Math.max(max, cal());
return;
}
// 상담 끝나는 날
int end = srcIdx+arr.get(srcIdx).day-1;
// 스케줄이 잡혀있지 않은 날이면서 상담이 끝나는 날이 퇴사 전날이면 상담 가능
if(!sch[srcIdx] && end <= N) {
select[srcIdx] = true;
for(int i=srcIdx; i<=end; i++) {
sch[i] = true;
}
search(srcIdx + 1);
select[srcIdx] = false;
for(int i=srcIdx; i<=end; i++) {
sch[i] = false;
}
search(srcIdx + 1);
} else {
search(srcIdx + 1);
}
}
// 소득 계산
static int cal() {
int sum = 0;
for(int i=1; i<=N; i++) {
if(select[i]) {
sum += arr.get(i).pay;
}
}
return sum;
}
}
class counsel {
int day;
int pay;
counsel(int day, int pay) {
this.day = day;
this.pay = pay;
}
}
|
cs |
'알고리즘 > 백준' 카테고리의 다른 글
| [JAVA] 백준 17471번 (0) | 2021.03.24 |
|---|---|
| [JAVA] 백준 14891번 (0) | 2021.03.16 |
| [JAVA] 백준 11653번 (0) | 2021.03.03 |
| [JAVA] 백준 4796번 (0) | 2020.11.06 |
| [JAVA] 백준 1026번 (0) | 2020.11.05 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 제네릭
- 11729
- 와일드카드
- 2529
- Wrapper Class
- 백준
- 디자인 패턴
- Regex
- 프로그래머스
- OOP
- 4796
- 순열
- 신규아이디추천
- generic
- 알고리즘
- 구간 합 구하기
- BFS
- 재귀
- 11659
- Stack
- 15686
- recursion
- 백트래킹
- 게리맨더링
- CS
- 하노이 탑
- gof
- java
- 래퍼 클래스
- 조합
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함