Submission #1106724

#TimeUsernameProblemLanguageResultExecution timeMemory
1106724luvnaSplit the sequence (APIO14_sequence)C++17
50 / 100
1249 ms131072 KiB
#include<bits/stdc++.h>

#define MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()
#define int long long

using namespace std;

template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}

typedef long long ll;
typedef long double ld;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand_range(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}

const int N = 1e5 + 15;

int n, k;
int pref[N];
ll dp[2][N];
bool past = false, succ = true;

struct line {
		ll m, c, id;
		ll compute(ll x) {return m * x + c;}
	};

	bool useless(line d1, line d2, line d3) {
		return (d1.c - d2.c) * (d3.m - d2.m) >= (d2.c - d3.c) * (d2.m - d1.m);
}

struct CHT {
	vector<line> lines;

	void reload() {
		while(!lines.empty()) lines.pop_back(); lines.push_back({0, 0, 0});
	}

	void add(line d) {
		while(lines.size() > 1 && useless(d, lines.back(), lines[lines.size() - 2]))
			lines.pop_back();
		lines.push_back(d);
	}

	pii query(ll x) {
		int l = 0, r = lines.size() - 1;
		while(l < r) {
			int m = (l + r) >> 1;
			if(lines[m].compute(x) <= lines[m + 1].compute(x)) l = m + 1;
			else r = m;
		}
		return {lines[l].compute(x), lines[l].id};
	}
};


CHT solver;
int trace[205][N];

void solve(){
    cin >> n >> k;

    for(int i = 1; i <= n; i++) cin >> pref[i], pref[i] += pref[i-1];

    for(int j = 1; j <= k; j++){
        solver.reload(); swap(succ, past);
        for(int i = 1; i <= n; i++){
            pii e = solver.query(pref[n] - pref[i]);
            trace[j][i] = e.se;
            dp[succ][i] = e.fi + 1LL*pref[i]*(pref[n] - pref[i]);
            solver.add({-pref[i], dp[past][i], i});
        }
    }

    int pos = -1;
    ll ans = -1;

    for(int i = 1; i <= n; i++){
        if(maximize(ans, dp[succ][i])) pos = i;
    }

    vector<int> result;
	for(int i = k; i >= 1; i--) {
		result.push_back(pos);
		pos = trace[i][pos];
	}

	cout << ans << endl;

	for(int i = k - 1; i >= 0; i--)
		cout << result[i] << " ";;
}

signed main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(0); cout.tie(0);

    #define task "task"

    if(fopen(task".INP", "r")){
        freopen(task".INP", "r", stdin);
        freopen(task".OUT", "w", stdout);
    }

    int t; t = 1; //cin >> t;
    while(t--) solve();
}

Compilation message (stderr)

sequence.cpp: In member function 'void CHT::reload()':
sequence.cpp:45:3: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
   45 |   while(!lines.empty()) lines.pop_back(); lines.push_back({0, 0, 0});
      |   ^~~~~
sequence.cpp:45:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
   45 |   while(!lines.empty()) lines.pop_back(); lines.push_back({0, 0, 0});
      |                                           ^~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |         freopen(task".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sequence.cpp:111:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  111 |         freopen(task".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...