# Luogu5155 [USACO18DEC]Balance Beam

## Description

1. 移动，$\frac{1}{2}$的概率往左移一个单位，$\frac{1}{2}$的概率往右移一个单位
2. 结束，你可以获得当前点上的权值$f_i$为收益后结束游戏

## Solution

$f(i) = \frac{f(i - 1)}{2} + \frac{f(i + 1)}{2}$

$w_i = \frac{f_b \cdot (i - a)}{b - a} + \frac{f_a \cdot (b - i)}{b - a}$

$w_i = \frac{f_b - f_a}{b - a} \cdot i + \frac{f_a \cdot b - f_b \cdot a}{b - a}$

## Code

#include <bits/stdc++.h>

using namespace std;

#define fst first
#define snd second
#define mp make_pair
#define squ(x) ((LL)(x) * (x))
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef pair<int, int> pii;

template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }

int sum = 0, fg = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') fg = -1;
for (; isdigit(c); c = getchar()) sum = (sum << 3) + (sum << 1) + (c ^ 0x30);
return fg * sum;
}

const int maxn = 1e5 + 10;

pii operator - (const pii &a, const pii &b) { return mp(a.fst - b.fst, a.snd - b.snd); }
LL operator * (const pii &a, const pii &b) { return (LL) a.fst * b.snd - (LL) a.snd * b.fst; }

inline bool cmp(const pii &a, const pii &b) {
if (a * b == 0) return a.fst > b.fst;
return a * b < 0;
}

pii A[maxn];

int main() {
freopen("balance.in", "r", stdin);
freopen("balance.out", "w", stdout);

for (int i = 1; i <= n; i++) A[i] = mp(i, read());
A[n + 1] = mp(n + 1, 0);

sort(A + 1, A + n + 2, cmp);

static int S[maxn], top = 0;
S[++top] = 0, S[++top] = 1;
for (int i = 2; i <= n + 1; i++) {
while (top > 1) {
if ((A[S[top]] - A[S[top - 1]]) * (A[i] - A[S[top]]) <= 0) break;
--top;
}
S[++top] = i;
}

for (int i = 2; i <= top && A[S[i - 1]].fst != n + 1; i++) {
int lst = A[S[i - 1]].fst, now = A[S[i]].fst;
for (int j = lst + 1; j <= now; j++)
if (j <= n) printf("%lld\n", ((LL) A[S[i]].snd * (j - lst) * (LL)1e5 + (LL) A[S[i - 1]].snd * (now - j) * (LL)1e5) / (now - lst));
}

return 0;
}

posted @ 2019-03-03 21:22  xunzhen  阅读(...)  评论(...编辑  收藏