forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1739.Building-Boxes.cpp
44 lines (38 loc) · 932 Bytes
/
1739.Building-Boxes.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
typedef long long ll;
class Solution {
public:
int minimumBoxes(int n)
{
int left = 1, right = 1e9;
while (left < right)
{
int mid = left+(right-left)/2;
if (cal(mid) >= n)
right = mid;
else
left = mid+1;
}
return left;
}
ll cal(ll area)
{
ll d = (int)sqrt(2*area);
while ((1+d)*d/2 > area)
d--;
ll diff = area - (1+d)*d/2;
vector<ll>a(d);
for (int i=0; i<d; i++)
a[i] = d - i;
for (int i=0; i<diff; i++)
a[i] += 1;
ll total = 0;
ll sufsum = 0;
for (int i=d-1; i>=0; i--)
{
sufsum += a[i];
total += sufsum;
}
// cout<<area<<" "<<d<<" "<<total<<endl;
return total;
}
};