Submission #54275

#TimeUsernameProblemLanguageResultExecution timeMemory
54275updown1Split the sequence (APIO14_sequence)C++17
71 / 100
2076 ms85364 KiB
/*
dp+convex hull trick
only add lines to the end of the hull bc slopes increase bc pre[i] increases
answer for i is >= answer for i-1
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i, a, b) for(int i=a; i<b; i++)
#define ffi For(i, 0, N)
#define ffj For(j, 0, K+1)
#define ffa ffi ffj
#define s <<" "<<
#define c <<" : "<<
#define w cout
#define e "\n"
#define pb push_back
#define mp make_pair
#define a first
#define b second
//#define int ll
//500,000,000 operations
const ll MAXN = 100000, INF = 100000000;
//Global Variables
int from[MAXN][201], prevv, N, K;
ll pre[MAXN], BIG=1000000000, dp[MAXN][2];
bool print;
vector<pair<ll, int> > hull; ///(start, ind)

struct Line { ///Ax+B
    ll A, B;
    Line() {A=0; B=0;}
    Line(int j, int line) {
        A = pre[j];
        B = dp[j][line%2]-pre[j]*pre[j];
        //w<< j s ":" s A s B<<e;
        //w<< dp[j][line%2]-pre[j]*pre[j] s B<<e;
    }
}val[MAXN];

pair<ll, int> getit(ll X) {
    while (prevv != hull.size()-1 && hull[prevv+1].a <= X) prevv++;
    int j = hull[prevv].b;
    //w<< X s j<<e;
    return mp(val[j].A*X+val[j].B, j);
}
bool below(int ind1, int ind2, ll X) {///Is ind1 below ind2 at location
    //w<< "Comparing" s ind1 s ind2 s "at location" s X<<e;//<< val[ind1].A*X+val[ind1].B s val[ind2].A*X+val[ind2].B <<e;
    return (val[ind1].A*X+val[ind1].B <= val[ind2].A*X+val[ind2].B);
}
void put(int ind, int line) {
    val[ind] = Line(ind, line);

    while (!hull.empty()) {
        int last = hull[hull.size()-1].b;
        ll loc = hull[hull.size()-1].a;
        if (below(last, ind, loc)) {
            hull.pop_back();
            if (prevv == hull.size()) prevv = 0;
        }
        else if (below(last, ind, BIG)) {
            /// split the range
            ll x = (val[last].B - val[ind].B)/(val[ind].A - val[last].A);
            hull.pb(mp(x+1, ind));
            break;
        }
        else break;
    }

    if (hull.empty()) {
        hull.pb(mp(0, ind));
        ///range: 0-BIG
    }
}

main() {
    //ifstream cin("test.in");
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> N >> K;
	cin >> pre[0];
	For (i, 1, N) {
        ll a; cin >> a; pre[i] = a+pre[i-1];
	}
	For (j, 1, K+1) {
        hull.clear();
        prevv = 0;
        put(j-1, j-1);
        For (i, j, N) {
            assert(pre[i] <= BIG);
            pair<ll, int> save = getit(pre[i]);
            dp[i][j%2] = save.a;
            //w<< "dp["<< i+1 <<"]["<<j<<"]:" s dp[i][j%2]<<e;
            from[i][j] = save.b;
            put(i, j-1);
            dp[i][(j-1)%2] = 0;
            //w<< "added" s i s j-1<<e; For(i, 0, hull.size()) {w<<hull[i].a s (i==hull.size()-1? BIG:hull[i+1].a-1) c hull[i].b <<e;}w<<e;
        }
	}
	//ffj {ffi w<< dp[i][j]<< " "; w<<e;}w<<e;
	//ffj {ffi w<< from[i][j]+1<< " "; w<<e;}w<<e;
	//ffi w<< pre[i]<<e;
	//For (i, 0, N-1) w<< dp[i][0]+pre[i]*(pre[N-1]-pre[i])<<" "; w<<e;

	w<< dp[N-1][K%2]<<e;
	ll at = N-1;for (ll j=K; j>=1; j--) {
	    assert(from[at][j] < at);
	    w<< from[at][j]+1<< " ";at = from[at][j];
    }w<<e;
}

Compilation message (stderr)

sequence.cpp: In function 'std::pair<long long int, int> getit(ll)':
sequence.cpp:42:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     while (prevv != hull.size()-1 && hull[prevv+1].a <= X) prevv++;
            ~~~~~~^~~~~~~~~~~~~~~~
sequence.cpp: In function 'void put(int, int)':
sequence.cpp:59:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if (prevv == hull.size()) prevv = 0;
                 ~~~~~~^~~~~~~~~~~~~~
sequence.cpp: At global scope:
sequence.cpp:76:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
#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...