This documentation is automatically generated by competitive-verifier/competitive-verifier
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/2603
// competitive-verifier: TLE 1
// competitive-verifier: MLE 64
#include <iostream>
#include <algorithm>
#include "src/Optimization/LiChaoTree.hpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int s, n, m;
cin >> s >> n >> m;
int x[s];
for (int i= 0; i < s; ++i) cin >> x[i];
int a[n];
for (int i= 0; i < n; ++i) {
int t, p;
cin >> t >> p;
a[i]= t - x[p - 1];
}
if (n <= m) return cout << 0 << '\n', 0;
sort(a, a + n);
int sum[n + 1];
sum[0]= 0;
for (int i= 0; i < n; ++i) sum[i + 1]= sum[i] + a[i];
auto w= [&](int i, int j) { return (i - j) * a[i - 1] - (sum[i] - sum[j]); };
int dp[n + 1];
fill_n(dp, n + 1, 1e9);
dp[0]= 0;
for (int _= m; _--;) {
LiChaoTree lct([&](int i, int j) { return dp[j] + w(i, j); }, 1, n + 1);
auto tree= lct.make_tree<MINIMIZE>();
int ndp[n + 1];
for (int i= 1; i <= n; ++i) {
tree.insert(i - 1);
ndp[i]= tree.query(i).first;
}
ndp[0]= 0, copy_n(ndp, n + 1, dp);
}
cout << dp[n] << '\n';
return 0;
}
#line 1 "test/aoj/2603.LiCT.test.cpp"
// competitive-verifier: PROBLEM https://onlinejudge.u-aizu.ac.jp/problems/2603
// competitive-verifier: TLE 1
// competitive-verifier: MLE 64
#include <iostream>
#include <algorithm>
#line 2 "src/Optimization/LiChaoTree.hpp"
#include <limits>
#line 4 "src/Optimization/LiChaoTree.hpp"
#include <vector>
#include <tuple>
#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 9 "src/Optimization/LiChaoTree.hpp"
template <class F, class T> class LiChaoTree {};
template <class F, class T, class... Prms> class LiChaoTree<F, std::tuple<T, Prms...>> {
using R= result_type_t<F>;
F f;
const T LB, UB;
std::vector<std::tuple<Prms...>> ps;
template <MinMaxEnum sgn, bool persistent> class LiChaoTreeInterface {
LiChaoTree *ins;
struct Node {
int id= -1;
Node *ch[2]= {nullptr, nullptr};
} *root;
inline R eval(int id, T x) const { return std::apply(ins->f, std::tuple_cat(std::make_tuple(x), ins->ps[id])); }
static inline bool cmp(const R &p, const R &n, int pi, int ni) {
if constexpr (sgn == MINIMIZE) return p > n || (p == n && pi > ni);
else return p < n || (p == n && pi > ni);
}
inline bool cmp(T x, int pi, int ni) const {
if (ni == -1) return false;
if (pi == -1) return true;
return cmp(eval(pi, x), eval(ni, x), pi, ni);
}
static inline T ub(T r) {
if constexpr (std::is_floating_point_v<T>) return r;
else return r - 1;
}
inline void addl(Node *&t, int id, T xl, T xr) {
if (!t) return t= new Node{id}, void();
bool bl= cmp(xl, t->id, id), br= cmp(ub(xr), t->id, id);
if (!bl && !br) return;
if constexpr (persistent) t= new Node(*t);
if (bl && br) return t->id= id, void();
T xm= (xl + xr) / 2;
if (cmp(xm, t->id, id)) std::swap(t->id, id), bl= !bl;
if constexpr (std::is_floating_point_v<T>)
if (xr - xl < 1e-9) return;
bl ? addl(t->ch[0], id, xl, xm) : addl(t->ch[1], id, xm, xr);
}
inline void adds(Node *&t, int id, T l, T r, T xl, T xr) {
if (r <= xl || xr <= l) return;
if (l <= xl && xr <= r) return addl(t, id, xl, xr);
if (!t) t= new Node;
else if constexpr (persistent) t= new Node(*t);
T xm= (xl + xr) / 2;
adds(t->ch[0], id, l, r, xl, xm), adds(t->ch[1], id, l, r, xm, xr);
}
inline std::pair<R, int> query(const Node *t, T x, T xl, T xr) const {
if (!t) return {R(), -1};
T xm= (xl + xr) / 2;
auto b= x < xm ? query(t->ch[0], x, xl, xm) : query(t->ch[1], x, xm, xr);
if (t->id == -1) return b;
R a= eval(t->id, x);
return b.second != -1 && cmp(a, b.first, t->id, b.second) ? b : std::make_pair(a, t->id);
}
public:
LiChaoTreeInterface()= default;
LiChaoTreeInterface(LiChaoTree *ins): ins(ins), root(nullptr) {}
void insert(const Prms &...args) { ins->ps.emplace_back(args...), addl(root, ins->ps.size() - 1, ins->LB, ins->UB); }
// [l, r)
void insert(const Prms &...args, T l, T r) {
l= std::max(l, ins->LB), r= std::min(r, ins->UB);
if (l < r) ins->ps.emplace_back(args...), adds(root, ins->ps.size() - 1, l, r, ins->LB, ins->UB);
}
// [l, UB)
void insert(const Prms &...args, T l) { insert(args..., l, ins->UB); }
std::pair<R, int> query(T x) const { return assert(ins->LB <= x && x < ins->UB), query(root, x, ins->LB, ins->UB); }
const std::tuple<Prms...> ¶ms(int id) const { return ins->ps[id]; }
};
public:
LiChaoTree(const F &f, T LB= -2e9, T UB= 2e9): f(f), LB(LB), UB(UB) {}
template <MinMaxEnum sgn= MINIMIZE, bool persistent= false> LiChaoTreeInterface<sgn, persistent> make_tree() { return this; }
};
template <class F, class T, class U> LiChaoTree(F, T, U) -> LiChaoTree<F, argument_type_t<F>>;
template <class F, class T> LiChaoTree(F, T) -> LiChaoTree<F, argument_type_t<F>>;
template <class F> LiChaoTree(F) -> LiChaoTree<F, argument_type_t<F>>;
#line 7 "test/aoj/2603.LiCT.test.cpp"
using namespace std;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int s, n, m;
cin >> s >> n >> m;
int x[s];
for (int i= 0; i < s; ++i) cin >> x[i];
int a[n];
for (int i= 0; i < n; ++i) {
int t, p;
cin >> t >> p;
a[i]= t - x[p - 1];
}
if (n <= m) return cout << 0 << '\n', 0;
sort(a, a + n);
int sum[n + 1];
sum[0]= 0;
for (int i= 0; i < n; ++i) sum[i + 1]= sum[i] + a[i];
auto w= [&](int i, int j) { return (i - j) * a[i - 1] - (sum[i] - sum[j]); };
int dp[n + 1];
fill_n(dp, n + 1, 1e9);
dp[0]= 0;
for (int _= m; _--;) {
LiChaoTree lct([&](int i, int j) { return dp[j] + w(i, j); }, 1, n + 1);
auto tree= lct.make_tree<MINIMIZE>();
int ndp[n + 1];
for (int i= 1; i <= n; ++i) {
tree.insert(i - 1);
ndp[i]= tree.query(i).first;
}
ndp[0]= 0, copy_n(ndp, n + 1, dp);
}
cout << dp[n] << '\n';
return 0;
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++-13 | 00_sample_00.txt |
![]() |
5 ms | 4 MB |
g++-13 | 00_sample_01.txt |
![]() |
5 ms | 4 MB |
g++-13 | 00_sample_02.txt |
![]() |
5 ms | 4 MB |
g++-13 | 01_teuchi_00.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_large_random_0.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_large_random_1.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_large_random_2.txt |
![]() |
16 ms | 6 MB |
g++-13 | 99_large_random_3.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_large_random_4.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_large_random_5.txt |
![]() |
5 ms | 3 MB |
g++-13 | 99_large_random_6.txt |
![]() |
26 ms | 9 MB |
g++-13 | 99_large_random_7.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_large_random_8.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_large_random_9.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_max_random_0.txt |
![]() |
109 ms | 16 MB |
g++-13 | 99_max_random_1.txt |
![]() |
70 ms | 10 MB |
g++-13 | 99_max_random_2.txt |
![]() |
154 ms | 22 MB |
g++-13 | 99_max_random_3.txt |
![]() |
201 ms | 30 MB |
g++-13 | 99_max_random_4.txt |
![]() |
162 ms | 24 MB |
g++-13 | 99_max_random_5.txt |
![]() |
172 ms | 25 MB |
g++-13 | 99_max_random_6.txt |
![]() |
245 ms | 37 MB |
g++-13 | 99_max_random_7.txt |
![]() |
312 ms | 49 MB |
g++-13 | 99_max_random_8.txt |
![]() |
11 ms | 4 MB |
g++-13 | 99_max_random_9.txt |
![]() |
19 ms | 4 MB |
g++-13 | 99_middle_random_0.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_middle_random_1.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_middle_random_2.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_middle_random_3.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_middle_random_4.txt |
![]() |
6 ms | 4 MB |
g++-13 | 99_middle_random_5.txt |
![]() |
7 ms | 4 MB |
g++-13 | 99_middle_random_6.txt |
![]() |
10 ms | 5 MB |
g++-13 | 99_middle_random_7.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_middle_random_8.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_middle_random_9.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_small_random_0.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_small_random_1.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_small_random_2.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_small_random_3.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_small_random_4.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_small_random_5.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_small_random_6.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_small_random_7.txt |
![]() |
4 ms | 4 MB |
g++-13 | 99_small_random_8.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_small_random_9.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_tiny_random_0.txt |
![]() |
4 ms | 3 MB |
g++-13 | 99_tiny_random_1.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_tiny_random_2.txt |
![]() |
4 ms | 4 MB |
g++-13 | 99_tiny_random_3.txt |
![]() |
4 ms | 4 MB |
g++-13 | 99_tiny_random_4.txt |
![]() |
5 ms | 3 MB |
g++-13 | 99_tiny_random_5.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_tiny_random_6.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_tiny_random_7.txt |
![]() |
5 ms | 4 MB |
g++-13 | 99_tiny_random_8.txt |
![]() |
5 ms | 3 MB |
g++-13 | 99_tiny_random_9.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 00_sample_00.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 00_sample_01.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 00_sample_02.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 01_teuchi_00.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_large_random_0.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_large_random_1.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_large_random_2.txt |
![]() |
16 ms | 6 MB |
clang++-18 | 99_large_random_3.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_large_random_4.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_large_random_5.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_large_random_6.txt |
![]() |
26 ms | 9 MB |
clang++-18 | 99_large_random_7.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_large_random_8.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_large_random_9.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_max_random_0.txt |
![]() |
112 ms | 16 MB |
clang++-18 | 99_max_random_1.txt |
![]() |
72 ms | 10 MB |
clang++-18 | 99_max_random_2.txt |
![]() |
158 ms | 22 MB |
clang++-18 | 99_max_random_3.txt |
![]() |
208 ms | 30 MB |
clang++-18 | 99_max_random_4.txt |
![]() |
168 ms | 24 MB |
clang++-18 | 99_max_random_5.txt |
![]() |
178 ms | 25 MB |
clang++-18 | 99_max_random_6.txt |
![]() |
254 ms | 37 MB |
clang++-18 | 99_max_random_7.txt |
![]() |
320 ms | 49 MB |
clang++-18 | 99_max_random_8.txt |
![]() |
11 ms | 4 MB |
clang++-18 | 99_max_random_9.txt |
![]() |
20 ms | 5 MB |
clang++-18 | 99_middle_random_0.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_middle_random_1.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_middle_random_2.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_middle_random_3.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_middle_random_4.txt |
![]() |
6 ms | 4 MB |
clang++-18 | 99_middle_random_5.txt |
![]() |
7 ms | 4 MB |
clang++-18 | 99_middle_random_6.txt |
![]() |
10 ms | 5 MB |
clang++-18 | 99_middle_random_7.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_middle_random_8.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_middle_random_9.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_small_random_0.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_small_random_1.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_small_random_2.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_small_random_3.txt |
![]() |
4 ms | 4 MB |
clang++-18 | 99_small_random_4.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_small_random_5.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_small_random_6.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_small_random_7.txt |
![]() |
4 ms | 4 MB |
clang++-18 | 99_small_random_8.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_small_random_9.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_tiny_random_0.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_tiny_random_1.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_tiny_random_2.txt |
![]() |
4 ms | 4 MB |
clang++-18 | 99_tiny_random_3.txt |
![]() |
4 ms | 4 MB |
clang++-18 | 99_tiny_random_4.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_tiny_random_5.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_tiny_random_6.txt |
![]() |
4 ms | 4 MB |
clang++-18 | 99_tiny_random_7.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_tiny_random_8.txt |
![]() |
5 ms | 4 MB |
clang++-18 | 99_tiny_random_9.txt |
![]() |
5 ms | 4 MB |