跳到正文

题解

题解 P3299 【[SDOI2013]保护出题人】

ModestCoder_约 3 分钟
原文
文章信息与版本

文章信息

发布
2019年8月12日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 3 分钟
获赞
22
题目
P3299

版本

  1. v1当前

这并不是一道dp

对于每个ii,答案是ansi=max(sumi−sumj−1xi+(i−j)d)ans_i=max(\frac{sum_i-sum_{j-1}}{x_i+(i-j)d}) 其中sumi=∑j=1iajsum_i=\sum_{j=1}^{i}a_j

可以斜率优化,因为答案的表达式可以看成(xi+i∗d,sumi)(j∗d,sumj−1)(x_i+i*d,sum_i)(j*d,sum_{j-1})两点的斜率

发现对于每个ii,(xi+i∗d,sumi)(x_i+i*d,sum_i)是个定点

所以对于点(j∗d,sumj−1)(j*d,sum_{j-1})维护一个下凸包,二分查找凸包中与定点相连斜率最大值

把每一个ii的答案累加起来即可

Code:

C++
#include <bits/stdc++.h>
#define maxn 100010
#define int long long
using namespace std;
struct data{
	int x, y;
}stk[maxn];
int n, D, sum[maxn], top;
double Ans;

inline int read(){
	int s = 0, w = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
	for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
	return s * w;
}

double slope(data x, data y){ return 1.0 * (x.y - y.y) / (x.x - y.x); }

signed main(){
	n = read(), D = read();
	stk[0] = (data){0, 0};
	for (int i = 1; i <= n; ++i){
		int x = read(), y = read(); sum[i] = sum[i - 1] + x;
		data tmp = {i * D, sum[i - 1]};
		while (top && slope(stk[top - 1], stk[top]) > slope(stk[top], tmp)) --top;
		stk[++top] = tmp;
		tmp = (data){y + i * D, sum[i]};
		int l = 1, r = top, ans = 0;
		while (l <= r){
			int mid = (l + r) >> 1;
			if (slope(stk[mid], tmp) > slope(stk[mid - 1], tmp)) ans = mid, l = mid + 1; else r = mid - 1;
		}
		Ans += slope(stk[ans], tmp);
	}
	printf("%.0f\n", Ans);
	return 0;
}
原文
存档评论4 条抓取于