Submission #409219

# Submission time Handle Problem Language Result Execution time Memory
409219 2021-05-20T11:25:56 Z Falcon Split the sequence (APIO14_sequence) C++17
100 / 100
988 ms 86508 KB
#include <bits/stdc++.h>

#ifdef DEBUG
#include "debug.hpp"
#endif

using namespace std;

#define all(c)              (c).begin(), (c).end()
#define rall(c)             (c).rbegin(), (c).rend()
#define traverse(c, it)     for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N)           for(int i = 0; i < (N); ++i)
#define rrep(i, N)          for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N)          for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e)       for(int i = (s); i <= (e); ++i)

#ifdef DEBUG
#define debug(x...)         { \
                            ++dbg::depth; \
                            string dbg_vals = dbg::to_string(x); \
                            --dbg::depth; \
                            dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
                            }

#define light_debug(x)      { \
                            dbg::light = true; \
                            dbg::dout << __func__ << ":" << __LINE__; \
                            dbg::dout << "  " << #x << " = " << x << endl; \
                            dbg::light = false; \
                            }

#else
#define debug(x...)         42
#define light_debug(x)      42
#endif


using ll = long long;

template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }

template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }

using point = complex<long long>;
namespace CHT {
    deque<point> v;
    deque<int> arg;

    long long dot(point p, point q) {
        return (conj(p) * q).real();
    }

    long long cross(point p, point q) {
        return (conj(p) * q).imag();
    }

    void clear() {
        v.clear(), arg.clear();
    }

    // assumes p.real is increasing.
    void add_line(point p, int i) {
        while(v.size() > 1) {
            point a{v[int(v.size()) - 1]}, b{v[int(v.size()) - 2]};
            if(cross(b - a, p - a) <= 0) v.pop_back(), arg.pop_back();
            else break;
        }
        v.push_back(p); arg.push_back(i);
    }

    // assumes x is increasing.
    pair<long long, int> get_best(long long x) {
        point p(x, 1);
        while(v.size() > 1 && dot(p, v[0]) <= dot(p, v[1]))
            v.pop_front(), arg.pop_front();
        return {dot(p, v[0]), arg[0]};
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, K; cin >> n >> K;
    vector<long long> p(n); rep(i, n) cin >> p[i]; partial_sum(all(p), p.begin());

    vector<long long> dp(n), dp_old(n);
    rep(i, n) dp_old[i] = p[i] * p[i];

    vector<vector<int>> pre(n, vector<int>(K));
    rep1(k, K) {
        CHT::clear();
        rep2(i, k, n - 1) {
            CHT::add_line(point(2 * p[i-1], -p[i-1] * p[i-1] - dp_old[i-1]), i-1);
            auto [v, j] = CHT::get_best(p[i]);
            dp[i] = p[i] * p[i] - v;
            pre[i][k - 1] = j;
        }
        debug(dp_old);
        dp_old = dp;
    }
    debug(dp_old);

    cout << (p.back() * p.back() - dp.back()) / 2 << '\n';
    
    int i{n - 1};
    rrep(k, K) {
        i = pre[i][k];
        cout << i + 1 << ' ';
    } 

    cout << '\n';
    
    
    #ifdef DEBUG
    dbg::dout << "\nExecution time: "
              << clock() * 1000 / CLOCKS_PER_SEC 
              << "ms" << endl;
    #endif
    return 0;
}


Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:33:29: warning: statement has no effect [-Wunused-value]
   33 | #define debug(x...)         42
      |                             ^~
sequence.cpp:101:9: note: in expansion of macro 'debug'
  101 |         debug(dp_old);
      |         ^~~~~
sequence.cpp:33:29: warning: statement has no effect [-Wunused-value]
   33 | #define debug(x...)         42
      |                             ^~
sequence.cpp:104:5: note: in expansion of macro 'debug'
  104 |     debug(dp_old);
      |     ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB contestant found the optimal answer: 108 == 108
2 Correct 1 ms 204 KB contestant found the optimal answer: 999 == 999
3 Correct 1 ms 320 KB contestant found the optimal answer: 0 == 0
4 Correct 1 ms 204 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 1 ms 316 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 1 ms 204 KB contestant found the optimal answer: 1 == 1
7 Correct 1 ms 204 KB contestant found the optimal answer: 1 == 1
8 Correct 1 ms 316 KB contestant found the optimal answer: 1 == 1
9 Correct 1 ms 316 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 1 ms 204 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 1 ms 204 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 1 ms 204 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 1 ms 204 KB contestant found the optimal answer: 140072 == 140072
14 Correct 1 ms 316 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 1 ms 204 KB contestant found the optimal answer: 805 == 805
16 Correct 1 ms 312 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 1 ms 320 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 204 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 1 ms 332 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 204 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 204 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 1 ms 204 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 204 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 1 ms 204 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 204 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 1 ms 204 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 332 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 2 ms 460 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 316 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 2 ms 332 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 2 ms 460 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 2 ms 460 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 1 ms 332 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 1 ms 332 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 1 ms 332 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 1 ms 332 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 7 ms 1180 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 1 ms 332 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 9 ms 1080 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 8 ms 956 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 10 ms 1088 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 7 ms 1100 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 3 ms 460 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 5 ms 588 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1100 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 4 ms 1228 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 85 ms 8928 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 4 ms 1216 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 62 ms 5836 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 62 ms 6424 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 70 ms 6796 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 50 ms 5680 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 61 ms 6476 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 74 ms 8012 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 30 ms 8896 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 36 ms 9292 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 824 ms 86508 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 30 ms 9668 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 988 ms 86112 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 684 ms 62376 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 710 ms 66064 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 538 ms 55108 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 610 ms 62660 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 791 ms 78320 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845