Submission #104508

#TimeUsernameProblemLanguageResultExecution timeMemory
104508E869120Split the sequence (APIO14_sequence)C++14
71 / 100
2094 ms63360 KiB
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct Node {
	long long l, r, a, b; int num;
};

bool operator<(const Node &a1, const Node &a2) {
	if (a1.l < a2.l) return true;
	return false;
}

class ConvexHullTrick {
public:
	vector<Node>vec;

	void init() {
		vec.push_back(Node{ -(1LL << 60), (1LL << 60), -(1LL << 30), (1LL << 60), -1LL });
	}

	long long cross_point(Node A, Node B) {
		if (A.a == B.a) {
			if (A.b < B.b) return (1LL << 59);
			return -(1LL << 59);
		}
		// A.a < B.a, 既に val の時点で B が下回っていることを表す
		long long E = A.b + (B.a - A.a) * (B.a - A.a) - B.b;
		long long F = 2LL * (B.a - A.a);
		if (E >= 0) return B.a - E / F;
		E *= -1;
		return B.a + (E + F - 1) / F;
	}
	
	void add(Node B, int id) {
		while (vec.size() >= 1) {
			long long P = cross_point(vec[vec.size() - 1], B);
			if (P > vec[vec.size() - 1].l) {
				vec[vec.size() - 1].r = P - 1;
				vec.push_back(Node{ P, (1LL << 60), B.a, B.b, (long long)id  });
				break;
			}
			else vec.pop_back();
		}
	}
	
	pair<long long, int> get_minimum(long long pos) {
		int pos1 = lower_bound(vec.begin(), vec.end(), Node{ pos + 1LL, -(1LL << 61), 0LL, 0LL, 0LL }) - vec.begin(); pos1--;
		return make_pair(vec[pos1].b + (vec[pos1].a - pos) * (vec[pos1].a - pos), vec[pos1].num);
	}
};

long long N, K, A1[100009], A2[100009], prev_[100009], dp[100009]; int pre[209][100009];

long long ranged(int l, int r) {
	long long V1 = A1[r] - A1[l - 1]; V1 *= V1;
	return V1;
}

long long solve() {
	prev_[0] = 0; for (int i = 1; i <= N; i++) prev_[i] = (1LL << 60);

	for (int i = 0; i < K; i++) {
		ConvexHullTrick G; G.init(); bool flag = false;
		for (int j = 0; j <= N; j++) {
			if (flag == true) {
				pair<long long, int> F = G.get_minimum(A1[j]);
				dp[j] = F.first;
				pre[i + 1][j] = F.second;
			}
			if (prev_[j] != (1LL << 60)) { G.add(Node{ -(1LL << 60), (1LL << 60), A1[j], prev_[j], j }, j); flag = true; }
		}
		for (int j = 0; j <= N; j++) { prev_[j] = dp[j]; dp[j] = (1LL << 60); }
	}
	return prev_[N];
}

int main() {
	cin >> N >> K; K++;
	for (int i = 1; i <= N; i++) { cin >> A1[i]; A2[i] = A1[i] * A1[i]; }
	for (int i = 1; i <= N; i++) { A1[i] += A1[i - 1]; A2[i] += A2[i - 1]; }
	cout << (ranged(1, N) - solve()) / 2LL << endl;
	vector<int>T; int cx = K, cy = N;
	while (cx >= 1) {
		if (cx < K) T.push_back(cy);
		cy = pre[cx][cy]; cx--;
	}
	for (int i = T.size() - 1; i >= 0; i--) { cout << T[i]; if (i) cout << " "; }
	cout << endl;
	return 0;
}

Compilation message (stderr)

sequence.cpp: In member function 'void ConvexHullTrick::add(Node, int)':
sequence.cpp:41:51: warning: narrowing conversion of '(long long int)id' from 'long long int' to 'int' inside { } [-Wnarrowing]
     vec.push_back(Node{ P, (1LL << 60), B.a, B.b, (long long)id  });
                                                   ^~~~~~~~~~~~~
#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...