-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemB.cpp
More file actions
74 lines (59 loc) · 1.69 KB
/
Copy pathProblemB.cpp
File metadata and controls
74 lines (59 loc) · 1.69 KB
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
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <sstream>
using namespace std;
bool canCut(const vector<double>& lengths, double length, int K) {
int count = 0;
for (double l : lengths) {
count += static_cast<int>(l / length);
if (count >= K) return true;
}
return false;
}
double maxCableLength(int N, int K, const vector<double>& lengths) {
double low = 0.01, high = *max_element(lengths.begin(), lengths.end());
double precision = 0.0001;
while (high - low > precision) {
double mid = (low + high) / 2;
if (canCut(lengths, mid, K)) {
low = mid;
}
else {
high = mid;
}
}
// Używamy low jako wyniku, ponieważ low będzie ostatnią długością, która spełnia warunek canCut
return floor(low * 100 + 0.5) / 100;
}
int main() {
int D;
cin >> D;
vector<string> results;
for (int i = 0; i < D; ++i) {
int N, K;
cin >> N >> K;
vector<double> lengths(N);
double total_length = 0.0;
for (int j = 0; j < N; ++j) {
cin >> lengths[j];
total_length += lengths[j];
}
if (total_length < K * 0.01) {
results.push_back("0.00");
}
else {
double max_length = maxCableLength(N, K, lengths);
ostringstream out;
out << fixed << setprecision(2) << max_length;
results.push_back(out.str());
}
}
// Wyświetlanie wyników po wprowadzeniu wszystkich danych
for (const string& result : results) {
cout << "Wynik: " << result << endl;
}
return 0;
}