Submission #1111981

#TimeUsernameProblemLanguageResultExecution timeMemory
1111981tintingyn21Split the sequence (APIO14_sequence)C++17
100 / 100
1339 ms90972 KiB
// author : daohuyenchi #include <bits/stdc++.h> using namespace std; #define ull unsigned long long #define db double #define i32 int32_t #define i64 int64_t #define ll long long // #define fi first #define se second // #define int long long // consider carefully // #define pii pair<int, int> #define pli pair<ll, int> #define pll pair<ll, ll> #define pil pair<int, ll> #define PAIR make_pair // TIME IS LIMITED ... #define rep(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define repd(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define repv(v, H) for(auto &v: H) // REFLECT ON THE PAST ... #define RESET(c, x) memset(c, x, sizeof(c)) #define MASK(i) (1LL << (i)) #define BIT(mask, i) (((mask) >> (i)) & 1LL) #define ONBIT(mask, i) ((mask) | (1LL << (i))) #define OFFBIT(mask, i) ((mask) &~ (1LL << (i))) #define COUNTBIT __builtin_popcountll // 30 / 1 / 2024 ? love is zero... start from zero #define PB push_back #define EB emplace_back #define vi vector<int> #define vll vector<ll> #define lwb lower_bound #define upb upper_bound #define all(v) (v).begin(), (v).end() #define special(H) (H).resize(distance(H.begin(), unique(all(H)))) // #define sp ' ' #define nl '\n' #define EL {cerr << '\n';} #define yes "YES" #define no "NO" #define Log2(n) (63 - __builtin_clzll(n)) #define left __left__ #define right __right__ //____________________________________________________________________ template <class X, class Y> bool maximize(X &a, const Y &b) { if(a < b) return a = b, true; return false; } template <class X, class Y> bool minimize(X &a, const Y &b) { if(a > b) return a = b, true; return false; } template <class... T> void print(T&&... n) { using exp = int[]; exp{0, (cerr << n << sp, 0)...}; cerr << nl; } template <class... T> void print2(T&&... n) { using exp = int[]; exp{0, (cerr << n << sp, 0)...}; // cerr << sp; } template <class T, class... C> void assign(int n, T v, C&&... a) { using e = int[]; e{(a.assign(n, v), 0)...}; } template <class... C> void resize(int n, C&&... a) { using e = int[]; e{(a.resize(n), 0)...}; } template <class T> using vector2d = vector<vector<T>>; template <class T> using vector3d = vector<vector2d<T>>; template <class T> int ssize(T &a) { return (int) a.size(); } //____________________________________________________________________ mt19937 rng(chrono::steady_clock().now().time_since_epoch().count()); const int MOD = 1000000007; // const int MOD[2] = {1000000009, 998244353}; template<class X> void modmize(X &x, int cur_Mod = MOD) { if(x >= cur_Mod) x -= cur_Mod; if(x < 0) x += cur_Mod; } const long long oo = 1e18 + 7; const int INF = 2e9; const int nmax = 2e5 + 10; const int MAX = 2e5; const int base = 311; const db eps = 1e-6; const int block = 500; static const double PI = acos(-1.0); //____________________________________________________________________ double division(ll a, ll b) { return 1.0 * a / b; } struct Line { ll m, c; int id; Line() {} Line(ll _m, ll _c, int _id) : m(_m), c(_c), id(_id) {} // y = m * x + c //. m is angle // if (m < 0) obtuse angle // else acute angle // imagine to ccw // if (m < 0) to close to 0, to large // if (m > 0) to far to 0, to large // y = m1 * x * c1 // y = m2 * x + c2 // c2 - c1 = x * (m1 - m2) double isect_X(const Line &ot) const { return division(ot.c - c, m - ot.m); } friend double isect_X(Line l1, Line l2) { return l1.isect_X(l2); } ; ll eval(ll x) { return m * x + c; } bool operator < (const Line &ot) { return m < ot.m; } } ; struct convex_hull_trick { // default for hull min // condition to use : all the line is sort by slope // if order goto is not sort, we use set instead by vector vector <Line> hull; vector <double> dot; void Reset() { hull.clear(); dot.clear(); dot.push_back(-oo); } void Init(Line L) { // hull.clear(); // dot.clear(); dot.push_back(-oo); hull.push_back(L); } bool check_bad(Line x, Line y, Line z) { // hull min condition : isect(x, y) > isect(x, z) // hull max condition : isect(x, y) < isect(x, z) return isect_X(x, y) >= isect_X(x, z); } void add_line(ll m, ll c, int id) { Line L = Line(m, c, id); // print(L.m, L.c, id); while (ssize(hull) > 1 && check_bad(hull[ssize(hull) - 2], hull.back(), L)) { hull.pop_back(); if (ssize(dot) > 0) dot.pop_back(); } if (hull.empty() == 0) { // assert(fabs(isect_X(L, hull.back()) - L.isect_X(hull.back())) <= eps) ; // cerr << fixed << setprecision(6) << L.isect_X(hull.back()) << nl; // cerr << fixed << setprecision(6) << isect_X(L, hull.back()) << nl; dot.push_back(L.isect_X(hull.back())); } hull.push_back(L); } pli cal(ll x) { // if (dot.empty() == 1) return ; int i = upper_bound(all(dot), x) - dot.begin() - 1; // cerr << i << ' ' << x << '\n'; // print(i, x, hull[i].m, hull[i].c); return PAIR(hull[i].eval(x), hull[i].id); } } ; int n, k; int a[nmax]; ll pf[nmax]; ll f[nmax], g[nmax]; int pre[nmax][205]; void tintingyn() { cin >> n >> k; rep (i, 1, n) { cin >> a[i]; pf[i] = pf[i - 1] + a[i]; } // { // vector2d <ll> dp(n + 2, vll(k + 2, -oo)); // dp[0][0] = 0; // rep (i, 1, n) { // rep (t, 1, min(k + 1, i)) { // rep (j, 1, i) { // maximize(dp[i][t], dp[j - 1][t - 1] + (pf[i] - pf[j - 1]) * pf[j - 1]); // } // } // } // cout << dp[n][k + 1] << nl; // // rep (i, 1, n) { // // cerr << dp[i][1] << sp; // // } // // EL // } rep (i, 1, n) { f[i] = g[i] = 0; } rep (_i, 2, k + 1) { rep (i, 1, n) { g[i] = f[i]; f[i] = -1e15; } convex_hull_trick cht; cht.Reset(); // cht.add_line(0, 0, 0); rep (i, 1, n) { if (i >= _i) { auto mx = cht.cal(pf[i]); f[i] = mx.fi; // print(_i, i, mx.fi); pre[i][_i] = mx.se; } cht.add_line(pf[i], - pf[i] * pf[i] + g[i], i); // print(_i, i); } } vector <int> List; { int x = n; int k2 = k + 1; while (x != 0) { List.push_back(x); x = pre[x][k2--]; } } cout << f[n] << nl; reverse(all(List)); List.pop_back(); repv (i, List) cout << i << sp; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //________________________________________________________________ #define TASK "3" if(fopen(TASK".inp", "r")) { freopen(TASK".inp", "r", stdin); freopen(TASK".out", "w", stdout); } //________________________________________________________________ // CODE FROM HERE ...! int num_test = 1; // cin >> num_test; while(num_test--) { tintingyn(); } cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" << nl; return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:316:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  316 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:317:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  317 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...