-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathSRM280-D1-500.cpp
81 lines (68 loc) · 1.57 KB
/
SRM280-D1-500.cpp
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
/*
Idea:
- Try to build the maximum full square starting from (0, 0).
- Then the answer is this square plus:
(1 full row or column + 1 non-ful row or column)
or
(1 non-ful row or column)
- Brute force to find the maximum full square.
*/
#include <bits/stdc++.h>
using namespace std;
class GridCut {
int solve(int width, int height, int n) {
int r1 = 0, r2 = 0;
if(width * height == n)
return 0;
if(width > height)
swap(width, height);
int rem = n % width;
if(n > width * height - width)
r1 = width - rem + 1;
else if(rem == 0)
r1 = width;
else
r1 = width + 1;
int mx = 0;
for(int i = 1; i <= width; ++i)
if(n >= i * i)
mx = i;
int tmp = n;
n -= mx * mx;
if(mx == width) {
n %= width;
if(tmp > width * height - width)
r2 = width - n + 1;
else if(n == 0)
r2 = width;
else
r2 = width + 1;
} else {
int plus = n >= mx;
if(plus)
n -= mx;
n = max(0, n);
if(mx + plus == width) {
if(mx + 1 == height) {
if(n == 0) {
r2 = width;
} else {
r2 = width - n + 1;
}
} else {
r2 = width + 1;
}
} else {
if(n == 0)
r2 = mx + (mx + plus);
else
r2 = mx + (mx + plus) + 1;
}
}
return min(r1, r2);
}
public:
int cutLength(int width, int height, int n) {
return min(solve(width, height, n), solve(width, height, width * height - n));
}
};