P3299 [SDOI2013]保护出题人

简单题意,不说。

可以看成$(x_i+i\times d,sum_i),(j\times d,sum_{j-1})$两点的斜率
询问不单调,三分凸包最大值。

代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define pii pair<int, int>
#define mk make_pair
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}
ll sum[N], a[N], x[N];
db ans = 0;
ll d;
db f(int i, int j)
{
return 1.00 * (sum[i] - sum[j - 1]) / (x[i] + (i - j) * d);
}
db slope(int i, int j)
{
return 1.00 * (sum[i - 1] - sum[j - 1]) / (i * d - j * d);
}
int q[N];
int main()
{
int n = read();
d = read();
for (int i = 1; i <= n; i++)
{
a[i] = read();
x[i] = read();
}
for (int i = 1; i <= n; i++)
sum[i] = a[i] + sum[i - 1];
int head = 1, tail = 0;
for (int i = 1; i <= n; i++)
{
while (head < tail && slope(q[tail], i) < slope(q[tail - 1], q[tail]))
tail--;
q[++tail] = i;
int l = 1, r = tail;
while (l != r)
{
int x = (r - l + 1) / 3;
int mid1 = l + x - 1, mid2 = l + 2 * x - 1;
if (mid1 == mid2)
break;
if (f(i, q[mid1]) < f(i, q[mid2]))
l = mid1 + 1;
else
r = mid2 - 1;
}

ans += max(f(i, q[l]), f(i, q[r]));
}
printf("%.0lf\n", ans);
}