-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflowers.cpp
40 lines (33 loc) · 830 Bytes
/
flowers.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
#include <bits/stdc++.h>
using namespace std;
double integrate(const double a, const double b, function<double(double)> f) {
const int n = 100'000;
const double dx = (b - a) / n;
double total = f(a) + f(b);
for (int i = 1; i < n; i++) {
const double mult = i % 2 == 0 ? 2 : 4;
total += mult * f(a + i * dx);
}
return (dx / 3.0) * total;
}
int main() {
double V;
int N;
cin >> V >> N;
int idx = -1;
double diff = DBL_MAX;
for (int i = 0; i < N; i++) {
double a, b, h;
cin >> a >> b >> h;
const double vol = integrate(0, h, [&](const double x) {
const double r = a * exp(-x * x) + b * sqrt(x);
return M_PI * r * r;
});
const double curr_diff = fabs(vol - V);
if (curr_diff < diff) {
diff = curr_diff;
idx = i;
}
}
cout << idx << endl;
}