-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCutSticks.cpp
52 lines (43 loc) · 1.05 KB
/
CutSticks.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
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
using namespace std;
class CutSticks {
public:
double maxKth(vector <int> sticks, int C, int K) {
double ret = 0;
double left = 0, right = 1e09, mid = 0;
for (int i=0; i<1000; i++) {
mid = (right + left) / 2;
int number_of_large_stick = 0, number_of_cut = 0;
int c = C;
for (int j=0; j<sticks.size(); j++) {
if (sticks[j] < mid) continue;
number_of_cut = (int)(max(sticks[j] / mid - 1.0, 0.0));
number_of_cut = min(number_of_cut, c);
c -= number_of_cut;
number_of_large_stick += number_of_cut + 1;
}
if (number_of_large_stick >= K)
left = mid;
else
right = mid;
}
ret = mid;
return ret;
}
};
// Powered by FileEdit
// Powered by TZTester 1.01 [25-Feb-2003]
// Powered by CodeProcessor