Submission #1055029

# Submission time Handle Problem Language Result Execution time Memory
1055029 2024-08-12T14:08:14 Z underwaterkillerwhale Split the sequence (APIO14_sequence) C++17
0 / 100
104 ms 11604 KB
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(),v.end()

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }

const int N = 2e5 + 7;
const int Mod = 1e9 + 7;
const ll INF = 1e18;
const ll BASE = 137;
const int szBL = 320;

int n, K;
ll a[N];
ll dp[N][2];
ll pre[N];

struct Line {
    #define ld long double
    ll a, b;
    ld rx;
    Line(ll aa = 0, ll bb = 0, ld _rx = INF) : a(aa), b(bb), rx(_rx) {};
    ll eval(ll x) {
        return a * x + b;
    }
    ld isectx(const Line &other) {
        assert(a != other.a);
        return 1.0 * (other.b - b) / (a - other.a);
    }
    bool operator < (const Line &other) const {
        if (other.a != INF) return a < other.a;
        return rx < other.rx;
    }
};

struct dynamicCHT {
    set<Line> hull;

    void init () {
        hull.clear();
    }

    bool hasPrev(set<Line>::iterator it) { return it != hull.begin(); }
    bool hasNext(set<Line>::iterator it) { return it != hull.end() && next(it) != hull.end(); }

    bool bad(Line l1, Line l2, Line l3) {
        return l2.isectx(l3) <= l1.isectx(l2);
    }

    bool bad(set<Line>::iterator it) {
        return (hasPrev(it) && hasNext(it) && bad(*prev(it), *it, *next(it)));
    }

    void calcrx(set<Line>::iterator it) {
        Line l = *it;
        if (hasNext(it)) l.rx = l.isectx(*next(it));
        else l.rx = INF;
        hull.erase(it); hull.insert(l);
    }

    void insert_Line(Line newLine) {
        auto it = hull.lower_bound(newLine);
        if (it != hull.end() && it->a == newLine.a) {
            if (it->b > newLine.b) return;
            hull.erase(it);
        }
        it = hull.insert(newLine).first;
        if (bad(it)) {
            hull.erase(it);
            return;
        }
        while (hasPrev(it) && bad(prev(it))) hull.erase(prev(it));
        while (hasNext(it) && bad(next(it))) hull.erase(next(it));
        if (hasPrev(it)) calcrx(prev(it));
        calcrx(it);
    }

    ll get (ll x) {
        if (hull.empty())
            return -INF;
        Line it = *hull.lower_bound(Line(INF, 0, x));
        return it.eval(x);
    }

} cht;

void solution () {
    cin >> n >> K;
    rep (i, 1, n) cin >> a[i];
    rep (i, 1, n) pre[i] = pre[i - 1] + a[i];
    rep (i, 1, n) dp[i][1] = 0;
    ///tinh th cuoi
    rep (k, 2, K) {
        cht.init();
        rep (i, 1, n) {
            dp[i][k & 1] = cht.get(pre[i]);
            cht.insert_Line(Line(pre[i], dp[i][k & 1 ^ 1] - pre[i] * pre[i]));
//            cout << k<<" "<<dp[i][k & 1] <<"\n";
        }
    }
    ll res = 0;
    rep (i, 1, n) res = max(res, dp[i][K & 1] + (pre[n] - pre[i]) * pre[i]);
    cout << res ;
}

#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
//    file("TREEV");
    ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
no bug challenge +2
2 + 8 * 2 - 9
7 3
4 1 3 4 0 2 3
*/

Compilation message

sequence.cpp: In function 'void solution()':
sequence.cpp:111:50: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  111 |             cht.insert_Line(Line(pre[i], dp[i][k & 1 ^ 1] - pre[i] * pre[i]));
      |                                                ~~^~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2396 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2392 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 2396 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 2396 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 7 ms 3304 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 104 ms 11604 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -