結果
| 問題 |
No.2970 三次関数の絶対値
|
| コンテスト | |
| ユーザー |
aqua
|
| 提出日時 | 2024-11-29 22:54:23 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,686 bytes |
| コンパイル時間 | 3,193 ms |
| コンパイル使用メモリ | 246,248 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-29 22:54:28 |
| 合計ジャッジ時間 | 3,907 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 41 WA * 9 |
ソースコード
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
using ll = long long;
int inf = 2e9;
ll infLL = 9e18;
template<class T> istream& operator>>(istream& i,vector<T>& v) {rep(j,v.size()) i>>v[j]; return i;}
template <typename T> void vecout(const vector<T> &v) {
rep(i, v.size()) {
cout << v[i];
cout << (i == v.size()-1 ? '\n' : ' ');
}
}
// ------------------------------------------------------------------------------------------------
vector<int> c(4);
double f(double &x) {
return c[0] + c[1] * x + c[2] * (x*x) + c[3] * (x*x*x);
}
double abs_f_min(vector<int> &c, double &l, double &r) {
bool minus = false, plus = false;
if(f(l) >= 0 || f(r) >= 0) plus = true;
if(f(l) <= 0 || f(r) <= 0) minus = true;
if(plus && minus) return 0;
double ans = numeric_limits<double>::infinity();
bool flag = false;
// a = 3 * c[3], b = 2 * c[2], c = c[1]
if(c[3] == 0) {
if(c[2] == 0) {
if(f(l) > 0) {
if(c[1] >= 0) ans = abs(f(l));
else ans = abs(f(r));
} else {
if(c[1] <= 0) ans = abs(f(l));
else ans = abs(f(r));
}
} else {
double x = -static_cast<double>(c[1]) / (2 * c[2]);
if(l <= x && x <= r) {
if(f(x) >= 0) plus = true;
if(f(x) <= 0) minus = true;
if(plus && minus) return 0;
ans = abs(f(x));
} else if(f(l) > 0) {
if(2 * c[2] * l + c[1] > 0) {
ans = abs(f(l));
} else if(2 * c[2] * l + c[1] < 0) {
ans = abs(f(r));
}
} else if(f(l) > 0) {
if(2 * c[2] * l + c[1] < 0) {
ans = abs(f(l));
} else if(2 * c[2] * l + c[1] > 0) {
ans = abs(f(r));
}
}
}
return ans;
} else {
int mid = -2 * c[2];
double D = sqrt(4 * (c[2] * c[2]) - 12 * c[3] * c[1]);
double x1 = (mid + D) / (6 * c[3]);
double x2 = (mid - D) / (6 * c[3]);
double y;
if(l <= x1 && x1 <= r) {
y = f(x1);
if(y >= 0) plus = true;
if(y <= 0) minus = true;
ans = min(ans, abs(y));
flag = true;
}
if(l <= x2 && x2 <= r) {
y = f(x2);
if(y >= 0) plus = true;
if(y <= 0) minus = true;
ans = min(ans, abs(y));
flag = true;
}
if(plus && minus) return 0;
else if(flag) return ans;
else return min(abs(f(l)), abs(f(r)));
}
}
void solve() {
cin >> c;
double l, r; cin >> l >> r;
cout << abs_f_min(c, l, r) << '\n';
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int t = 1; // cin >> t;
rep(_,t) solve();
}
aqua