This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/180
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#include <vector>
#include "src/Optimization/fibonacci_search.hpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
int N;
cin >> N;
long long a[N], b[N];
for (int i= 0; i < N; ++i) cin >> a[i] >> b[i];
auto f= [&](long long n) {
long long mx= 0, mi= 1e18;
for (int i= 0; i < N; ++i) {
long long tmp= a[i] + b[i] * n;
mx= max(mx, tmp);
mi= min(mi, tmp);
}
return (mx - mi) * 2 + n;
};
auto [x, fx]= fibonacci_search<MINIMIZE>(f, 1, 1e9);
cout << x;
return 0;
}
#line 1 "test/yukicoder/180.test.cpp"
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/180
// competitive-verifier: TLE 0.5
// competitive-verifier: MLE 64
#include <iostream>
#include <vector>
#line 2 "src/Optimization/fibonacci_search.hpp"
#include <algorithm>
#include <cassert>
#line 2 "src/Internal/function_traits.hpp"
#include <type_traits>
// clang-format off
namespace function_template_internal{
template<class C>struct is_function_object{
template<class U,int dummy=(&U::operator(),0)> static std::true_type check(U *);
static std::false_type check(...);
static C *m;
static constexpr bool value= decltype(check(m))::value;
};
template<class F,bool,bool>struct function_type_impl{using type= void;};
template<class F>struct function_type_impl<F,true,false>{using type= F *;};
template<class F>struct function_type_impl<F,false,true>{using type= decltype(&F::operator());};
template<class F> using function_type_t= typename function_type_impl<F,std::is_function_v<F>,is_function_object<F>::value>::type;
template<class... Args>struct result_type_impl{using type= void;};
template<class R,class... Args>struct result_type_impl<R(*)(Args...)>{using type= R;};
template<class C,class R,class... Args>struct result_type_impl<R(C::*)(Args...)>{using type= R;};
template<class C,class R,class... Args>struct result_type_impl<R(C::*)(Args...)const>{using type= R;};
template<class F> using result_type_t= typename result_type_impl<function_type_t<F>>::type;
template<class... Args>struct argument_type_impl{using type= void;};
template<class R,class... Args>struct argument_type_impl<R(*)(Args...)>{using type= std::tuple<Args...>;};
template<class C,class R,class... Args>struct argument_type_impl<R(C::*)(Args...)>{using type= std::tuple<Args...>;};
template<class C,class R,class... Args>struct argument_type_impl<R(C::*)(Args...)const>{using type= std::tuple<Args...>;};
template<class F> using argument_type_t= typename argument_type_impl<function_type_t<F>>::type;
}
using function_template_internal::result_type_t,function_template_internal::argument_type_t;
// clang-format on
#line 2 "src/Optimization/MinMaxEnum.hpp"
enum MinMaxEnum { MAXIMIZE= -1, MINIMIZE= 1 };
#line 6 "src/Optimization/fibonacci_search.hpp"
// [l,r]
template <MinMaxEnum obj, class F> std::pair<int64_t, result_type_t<F>> fibonacci_search(const F &f, int64_t l, int64_t r) {
assert(l <= r);
int64_t s= 1, t= 2, a= l - 1, x, b, y;
for (int64_t e= r - l + 2; t < e;) std::swap(s+= t, t);
b= a + t, x= b - s;
result_type_t<F> fx= f(x), fy;
for (bool g; a + b != 2 * x;) {
if (y= a + b - x; r < y) b= a, a= y;
else {
if constexpr (obj == MINIMIZE) g= fx < (fy= f(y));
else g= fx > (fy= f(y));
if (g) b= a, a= y;
else a= x, x= y, fx= fy;
}
}
return {x, fx};
}
#line 7 "test/yukicoder/180.test.cpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
int N;
cin >> N;
long long a[N], b[N];
for (int i= 0; i < N; ++i) cin >> a[i] >> b[i];
auto f= [&](long long n) {
long long mx= 0, mi= 1e18;
for (int i= 0; i < N; ++i) {
long long tmp= a[i] + b[i] * n;
mx= max(mx, tmp);
mi= min(mi, tmp);
}
return (mx - mi) * 2 + n;
};
auto [x, fx]= fibonacci_search<MINIMIZE>(f, 1, 1e9);
cout << x;
return 0;
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++-13 | 00_sample1.txt |
![]() |
7 ms | 4 MB |
g++-13 | 00_sample2.txt |
![]() |
6 ms | 4 MB |
g++-13 | 00_sample3.txt |
![]() |
6 ms | 4 MB |
g++-13 | 00_sample4.txt |
![]() |
6 ms | 4 MB |
g++-13 | 10_randomA1.txt |
![]() |
6 ms | 4 MB |
g++-13 | 10_randomA2.txt |
![]() |
6 ms | 4 MB |
g++-13 | 10_randomA3.txt |
![]() |
6 ms | 4 MB |
g++-13 | 10_randomA4.txt |
![]() |
6 ms | 4 MB |
g++-13 | 10_randomA5.txt |
![]() |
6 ms | 4 MB |
g++-13 | 11_randomB1.txt |
![]() |
6 ms | 4 MB |
g++-13 | 11_randomB2.txt |
![]() |
6 ms | 4 MB |
g++-13 | 12_randomC1.txt |
![]() |
6 ms | 4 MB |
g++-13 | 12_randomC2.txt |
![]() |
6 ms | 4 MB |
g++-13 | 12_randomC3.txt |
![]() |
6 ms | 4 MB |
g++-13 | 12_randomC4.txt |
![]() |
6 ms | 4 MB |
g++-13 | 12_randomC5.txt |
![]() |
6 ms | 4 MB |
g++-13 | 12_randomC6.txt |
![]() |
6 ms | 4 MB |
g++-13 | 12_randomC7.txt |
![]() |
6 ms | 4 MB |
g++-13 | 12_randomC8.txt |
![]() |
6 ms | 4 MB |
g++-13 | 12_randomC9.txt |
![]() |
6 ms | 4 MB |
g++-13 | 20_special01.txt |
![]() |
6 ms | 4 MB |
g++-13 | 20_special02.txt |
![]() |
6 ms | 4 MB |
g++-13 | 20_special03.txt |
![]() |
5 ms | 4 MB |
g++-13 | 20_special04.txt |
![]() |
5 ms | 4 MB |
g++-13 | 20_special05.txt |
![]() |
5 ms | 4 MB |
g++-13 | 20_special06.txt |
![]() |
5 ms | 4 MB |
g++-13 | 20_special07.txt |
![]() |
6 ms | 4 MB |
g++-13 | 20_special08.txt |
![]() |
5 ms | 4 MB |
g++-13 | 20_special09.txt |
![]() |
6 ms | 4 MB |
g++-13 | 20_special10.txt |
![]() |
5 ms | 4 MB |
g++-13 | 20_special11.txt |
![]() |
6 ms | 4 MB |
g++-13 | 20_special12.txt |
![]() |
6 ms | 4 MB |
g++-13 | 20_special13.txt |
![]() |
6 ms | 4 MB |
g++-13 | 20_special14.txt |
![]() |
6 ms | 4 MB |
g++-13 | 20_special15.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 00_sample1.txt |
![]() |
7 ms | 4 MB |
clang++-18 | 00_sample2.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 00_sample3.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 00_sample4.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 10_randomA1.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 10_randomA2.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 10_randomA3.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 10_randomA4.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 10_randomA5.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 11_randomB1.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 11_randomB2.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 12_randomC1.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 12_randomC2.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 12_randomC3.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 12_randomC4.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 12_randomC5.txt |
![]() |
11 ms | 4 MB |
clang++-18 | 12_randomC6.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 12_randomC7.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 12_randomC8.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 12_randomC9.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 20_special01.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 20_special02.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 20_special03.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 20_special04.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 20_special05.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 20_special06.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 20_special07.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 20_special08.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 20_special09.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 20_special10.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 20_special11.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 20_special12.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 20_special13.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 20_special14.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 20_special15.txt |
![]() |
6 ms | 4 MB |