-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q63.java
81 lines (70 loc) · 2.26 KB
/
Q63.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
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;
public class Q63 {
static Set<LocalDate> holidays = new HashSet<>();
static final int MAX = 5;
static final int HEIGHT = 6;
static final int WIDTH = 7;
public static void main (String[] args) throws IOException {
long s = System.currentTimeMillis();
BufferedReader br =
new BufferedReader(new FileReader("C:\\Users\\hangn.DESKTOP-TAM40T8\\Downloads\\sample\\q63.txt"));
String line = "";
while ((line = br.readLine()) != null) {
String[] ary = line.split("/");
int year = Integer.parseInt(ary[0]);
int month = Integer.parseInt(ary[1]);
int day = Integer.parseInt(ary[2]);
holidays.add(LocalDate.of(year, month, day));
}
int ans = 0;
for (int year = 2006; year <= 2015; year++) {
for (int month = 1; month <= 12; month++)
ans += solve(year, month);
}
br.close();
System.out.println(ans);
long e = System.currentTimeMillis();
System.out.println("time:" + (e-s) + "ms");
}
static int solve (int year, int month) {
boolean[][] is_not_holiday = new boolean[HEIGHT][WIDTH];
LocalDate start = LocalDate.of(year, month, 1);
LocalDate lim = start.plusMonths(1);
int height = 0;
for (; start.isBefore(lim); start = start.plusDays(1)) {
int val = start.getDayOfWeek().getValue() % 7;
is_not_holiday[height][val] = !holidays.contains(start) && (val != 0 && val != 6);
if (val == WIDTH - 1) height++;
}
return find_maximum_square(is_not_holiday);
}
static int find_maximum_square (boolean[][] ary) {
int ret = 0;
// 開始位置
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
if (!ary[y][x]) continue;
// 長方形の縦横
for (int h = 1; h <= MAX; h++) {
loop_w:
for (int w = 1; w <= MAX; w++) {
for (int hi = 0; hi < h; hi++) {
if (y + hi >= HEIGHT) continue loop_w;
for (int wi = 0; wi < w; wi++) {
// はみだしたか、土日祝日
if (x + wi >= WIDTH || !ary[y + hi][x + wi]) continue loop_w;
}
}
ret = Math.max(ret, h * w);
}
}
}
}
return ret;
}
}