50 分求助
查看原帖
50 分求助
110111
jyttoby楼主2020/5/17 19:46
#include <cctype>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;

void Readint(int &v) {
	char c;
	bool neg = false;
	while (!isdigit(c = cin.get()))
		if (c == '-') neg = true;
	v = c & 15;
	while (isdigit(c = cin.get())) v = v*10 + (c & 15);
	if (neg) v = -v;
}

deque<int> q;

#define Empty q.empty
#define PushFront q.push_front
#define PushBack q.push_back
#define PopFront q.pop_front
#define PopBack q.pop_back

inline int First() {
	if (Empty()) return 0;
	return q.front();
}

inline int Last() {
	if (Empty()) return 0;
	return q.back();
}

inline int Second() {
	int fi_t = q.front();
	PopFront();
	int se_t = First();
	PushFront(fi_t);
	return se_t;
}

inline int SecLast() {
	int fi_t = q.back();
	PopBack();
	int se_t = Last();
	PushBack(fi_t);
	return se_t;
}

const int MaxN = 1001000;

int x[MaxN];
int s[MaxN];
long long f[MaxN];
int a;

inline long long Numer(int x1, int x2) {
	return f[x1] + (long long)a * s[x1] * s[x1]
		 - f[x2] - (long long)a * s[x2] * s[x2];
}

inline long long Denom(int x1, int x2) {
	return s[x1] - s[x2];
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, b, c;
	Readint(n);
	Readint(a);
	Readint(b);
	Readint(c);
	for_each(x + 1, x + n + 1, Readint);
	for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + x[i];
	PushBack(0);
	for (int i = 1; i <= n; ++i) {
		while (Empty() == false && 
			   Numer(First(), Second()) <=
			   (b + 2*a*s[i]) * Denom(First(), Second()))
			PopFront();
		int j = First();
		f[i] = f[j] + (long long)a * (s[i] - s[j]) * (s[i] - s[j]) +
			   (long long)b * (s[i] - s[j]) + c;
		while (Empty() == false &&
			   Numer(SecLast(), Last()) * Denom(Last(), i) <=
			   Numer(Last(), i) * Denom(SecLast(), Last()))
			PopBack();
		PushBack(i);
	}
	cout << f[n] << endl;
	return 0;
}
2020/5/17 19:46
加载中...