結果
| 問題 |
No.2970 三次関数の絶対値
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-11-29 23:03:04 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 1,426 bytes |
| コンパイル時間 | 2,112 ms |
| コンパイル使用メモリ | 200,728 KB |
| 最終ジャッジ日時 | 2025-02-26 09:28:30 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 50 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
void solve() {
ll C0, C1, C2, C3, L, R;
cin >> C0 >> C1 >> C2 >> C3 >> L >> R;
auto f = [&](double x) -> double {
double res = double(C0);
res += double(C1) * x;
res += double(C2) * x * x;
res += double(C3) * x * x * x;
return res;
};
vector<double> xs;
if (C3 != 0) {
ll a = 3 * C3, b = 2 * C2, c = C1;
ll d = b * b - 4 * a * c;
if (d >= 0) {
double z = sqrt(double(d));
double x0 = (-double(b) + z) / (2.0 * double(a));
double x1 = (-double(b) - z) / (2.0 * double(a));
if (L <= x0 && x0 <= R)
xs.push_back(x0);
if (L <= x1 && x1 <= R)
xs.push_back(x1);
}
} else if (C2 != 0) {
double x0 = (-double(C1) / double(2 * C2));
if (L <= x0 && x0 <= R)
xs.push_back(x0);
}
xs.push_back(L);
xs.push_back(R);
sort(xs.begin(), xs.end());
double ans = 1e18;
rep(i, xs.size() - 1) {
double y0 = f(xs[i]), y1 = f(xs[i + 1]);
if ((y0 <= 0 && 0 <= y1) || (y0 >= 0 && 0 >= y1)) {
ans = 0;
} else {
ans = min(ans, abs(y0));
ans = min(ans, abs(y1));
}
}
cout << fixed << setprecision(6) << ans << '\n';
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
int T = 1;
for (int t = 0; t < T; t++) {
solve();
}
return 0;
}