제출 #461255

#제출 시각아이디문제언어결과실행 시간메모리
461255hhhhauraSplit the sequence (APIO14_sequence)C++14
100 / 100
445 ms88680 KiB
#define wiwihorz
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#include <bits/stdc++.h>

#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))

#define INF 1000000000000000000
#define MOD 1000000007
#define eps (1e-9)

using namespace std;

#define lld long double
#define int long long int
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())

#ifdef wiwihorz
#define print(a...) cerr << "Line " << __LINE__ <<": ", kout("[" + string(#a) + "] = ", a)
void kout() {cerr << endl;}
void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
template<class T1, class ... T2> void kout(T1 a, T2 ... e) { cerr << a << " ", kout(e...); }
#else
#define print(...) 0
#define vprint(...) 0
#endif
#define a first
#define b second
namespace solver {
	int n, k;
	vector<int> dp[2], pre, a, pos;
	vector<vector<signed>> rec;
	vector<signed> ans;
	int get_val(pii p, int x) {
		return p.a * x + p.b;	
	}
	bool check(pii i, pii j, pii k) {
		return (i.a - j.a) * (k.b - i.b) <
			(j.b - i.b) * (i.a - k.a);
	}	
	void init_(int _n, int _k) {
		n = _n, k = _k;
		dp[0].assign(n + 1, 0);
		dp[1].assign(n + 1, 0);
		pre.assign(n + 1, 0);
		a.assign(n + 1, 0);
		ans.assign(n + 1, 0);
		rec.assign(k + 10, vector<signed>(n + 1, 0));
	}
	void output(int y, int x) {	
		if(y != 2) output(y - 1, rec[y][x]);
		cout << rec[y][x] << " \n"[y == k + 1];
	}
	void solve() {
		vector<pii> q(n + 5, {0, 0});
		pos.assign(n + 5, 0); 
		rep(i, 1, n) pre[i]  = pre[i - 1] + a[i];
		rep(l, 1, k + 1) {
			int id = l & 1, L = 1, R = 1;
			q[1] = {-pre[l - 1], dp[!id][l - 1]};
			pos[1] = l - 1;
			rep(i, l, n) {
				int suf = pre[n] - pre[i];
				while(L < R && get_val(q[L], suf) <=
					get_val(q[L + 1], suf)) L ++;
				dp[id][i] = get_val(q[L], suf) + pre[i] * suf;
				rec[l][i] = pos[L];
				pii cur = {-pre[i], dp[!id][i]};
				while(R > L && !check(q[R - 1], q[R], cur)) R--;
				q[++R] = cur, pos[R] = i;
			}
		}
		cout << dp[(k + 1) & 1][n] << "\n";
		output(k + 1, n);
	}
};
using namespace solver;
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int n, k;
	cin >> n >> k;
	init_(n, k);
	rep(i, 1, n) cin >> a[i];
	solve();
	return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

sequence.cpp:3: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
    3 | #pragma loop-opt(on)
      | 
sequence.cpp:25:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   25 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
      |             ^~~~
sequence.cpp:25:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
   25 | void vprint(auto L, auto R) { while(L < R) cerr << *L << " \n"[next(L) == R], ++L;}
      |                     ^~~~
#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...