Submission #938365

#TimeUsernameProblemLanguageResultExecution timeMemory
938365vjudge1수열 (APIO14_sequence)C++17
Compilation error
0 ms0 KiB
#include<bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"

const ld PI = 3.1415926535;
const int N = 100000+1;
const int M = 7e6 + 1;
int mod = 998244353;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
const int P = 31;

int mult(int a, int b) {
    return a * 1LL * b % mod;
}

int sum(int a, int b) { 
    if (a + b < 0)
        return a + b + mod;
    if (a + b >= mod)
        return a + b - mod;
    return a + b;
}

ll binpow(ll a, ll n) {
    if (n == 0)
        return 1;
    if (n % 2 == 1) {
        return binpow(a, n - 1) * a % mod;
    } else {
        ll b = binpow(a, n / 2);
        return b * b % mod;
    }
}

struct Line {
	mutable ll k, m, p, ind;
	bool operator<(const Line& o) const { return k < o.k; }
	bool operator<(ll x) const { return p < x; }
};

struct LineContainer : multiset<Line, less<>> {
	// (for doubles, use inf = 1/.0, div(a,b) = a/b)
	static const ll inf = LLONG_MAX;
	ll div(ll a, ll b) { // floored division
		return a / b - ((a ^ b) < 0 && a % b); }
	bool isect(iterator x, iterator y) {
		if (y == end()) return x->p = inf, 0;
		if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
		else x->p = div(y->m - x->m, x->k - y->k);
		return x->p >= y->p;
	}
	void add(ll k, ll m, ll ind) {
		auto z = insert({k, m, 0, ind}), y = z++, x = y;
		while (isect(y, z)) z = erase(z);
		if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
		while ((y = x) != begin() && (--x)->p >= y->p)
			isect(x, erase(y));
	}
	pii query(ll x) {
		assert(!empty());
		auto l = *lower_bound(x);
		return {l.k * x + l.m, l.ind};
	}
};

int a[N], pr[N], p[N][201];
vector<int> cdp(N, 0), pdp(N, 0);
void solve(){
    int n, k;
    cin >> n >> k;
    pr[0] = 0;
    for(int i = 1; i <= n; i++)
        cin >> a[i], pr[i] = pr[i - 1] + a[i];
    
    for(int l = 1; l <= k; l++){
        LineContainer lc;
        lc.add(pr[l], pdp[l] - pr[l] * pr[l], l);
        cdp[l] = 0;
        p[l][l] = 0;
        for(int i = l + 1; i <= n; i++){
            pii ret = lc.query(pr[i]);
            cdp[i] = ret.F;
            p[i][l] = ret.S;
            lc.add(pr[i], pdp[i] - pr[i] * pr[i], i);
        }
        pdp = cdp;
    }
    int cur = k, cn = n;
    vector<int> ans = {};
    while(cur > 0){
        ans.pb(p[cn][cur]);
        cn = p[cn][cur];
        cur--;
    }
    cout << dp[n][k] << '\n';
    reverse(all(ans));
    for(auto e : ans)
        cout << e << ' ';
}   

signed main() {
    mispertion;
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

Compilation message (stderr)

sequence.cpp: In function 'void solve()':
sequence.cpp:113:13: error: 'dp' was not declared in this scope; did you mean 'pdp'?
  113 |     cout << dp[n][k] << '\n';
      |             ^~
      |             pdp