Submission #1095648

#TimeUsernameProblemLanguageResultExecution timeMemory
1095648akim9905수열 (APIO14_sequence)C++17
100 / 100
290 ms83948 KiB
#include <bits/stdc++.h>
using namespace std;

#define fileio() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define fio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define allr(x) x.rbegin(), x.rend()
#define cmprs(x) sort(all(x)),x.erase(unique(all(x)),x.end())
#define endl "\n"
#define sp " "
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define rz resize
#define sz(a) (int)(a.size())
#define clr(a) a.clear()

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef tuple<int, int, int> tpi;
typedef tuple<ll, ll, ll> tpl;
typedef pair<double, ll> pdl;
typedef pair<double, int> pdi;

const int dx[] = {1,-1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 101010; // PLZ CHK!
const int MAXM = 202;

struct CHT{
    ll la[MAXN], lb[MAXN]; int lz[MAXN];
    int stkn=0,pt=1;
    void upd(ll a, ll b, int z){
        while(stkn>pt){
            if(a==la[stkn] && b>=lb[stkn]) stkn--;
            else if((lb[stkn]-b)*(la[stkn]-la[stkn-1])<(a-la[stkn])*(lb[stkn-1]-lb[stkn])) stkn--;
            else break;
        }
        if(a==la[stkn] && b>=lb[stkn] && z!=0) return;
        la[++stkn]=a;
        lb[stkn]=b;
        lz[stkn]=z;
    }
    pll qry(ll x){
        while(pt<stkn && (lb[pt]-lb[pt+1])<(la[pt+1]-la[pt])*x) pt++;
        return {la[pt]*x+lb[pt],lz[pt]};
    }
};

int N,M;
ll P[MAXN],D[2][MAXN];
int par[MAXM][MAXN];

int main() {
    fio();
    cin>>N>>M;
    for (int i=1; i<=N; i++) cin>>P[i], P[i]+=P[i-1];

    for (int j=1; j<=M; j++) {
        CHT cht;
        for (int i=1; i<=N; i++) {
            cht.upd(P[i-1],-P[i-1]*P[i-1]+D[(j-1)&1][i-1],i-1); 
            auto [mx,mxi]=cht.qry(P[i]);
            D[j&1][i]=mx;
            par[j][i]=mxi;
        }
    }

    cout<<D[M&1][N]<<endl;
    vector<int> idx; idx.pb(-1);
    for (int x=N,y=M; y>=1; y--) {
        idx.pb(par[y][x]);
        x=par[y][x];
    }
    sort(all(idx));
    for (int i=1; i<=M; i++) {
        if (idx[i]==0) idx[i]=1;
        if (idx[i]<=idx[i-1]) idx[i]=idx[i-1]+1;
        cout<<idx[i]<<sp;
    }

    return 0;
}

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:46:22: warning: 'cht.CHT::la[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
   46 |         if(a==la[stkn] && b>=lb[stkn] && z!=0) return;
      |               ~~~~~~~^
#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...