题意:给你几个点组成山脉的纵切面,一个横坐标对应一个y坐标,而且x递增给出。求一个长度为W的区间,让区间中的山的平均高度最大(即总高度最大)
题解:
这个题写崩了,虽然用了线段树,但是预处理前缀和就可以了。主要是比赛时候有点懵了。
我们考虑:如果区间的两个端点都在山坡上(不跨越任何顶点),那么随着区间的滑动,总面积大小是一个单峰的,或者单调的函数。所有我们可以枚举每个不跨越节点区间段,然后执行三分。具体操作:
int l = lisan[1];
int r = l - m;
while(r <= lisan[n])
{
int lr = upper_bound(lisan+1, lisan+n+1, l) - lisan;
int rc = upper_bound(lisan+1, lisan+n+1, r) - lisan;
int gg = min(lisan[lr] - l, lisan[rc] - r);
//........
//执行三分
//........
r += gg; l += gg;
}
这样就枚举了所有区间段,对于区间求和,我们可以这么做:首先对于一个区间,他肯定包含了顶点之间的和端点到顶点之间的面积,如图:
红线之间是要求的面积,那么,S1我们可以通过预处理前缀和或者线段树求出来,然后可以计算S2和S3的面积。
下面是ac代码(因为昨天实在是懵了,代码写的很糊,不建议参考):
#include<iostream>
#include<cstring>
#include <map>
#include <queue>
#include <set>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <bitset>
#include <array>
#include <cctype>
#include <time.h>
#pragma GCC optimize(2)
void read_f() { freopen("1.in", "r", stdin); freopen("1.out", "w", stdout); }
void fast_cin() { std::ios::sync_with_stdio(false); std::cin.tie(); }
void run_time() { std::cout << "ESC in : " << clock() * 1000.0 / CLOCKS_PER_SEC << "ms" << std::endl; }
template <typename T>
bool bacmp(const T & a, const T & b) { return a > b; }
template <typename T>
bool pecmp(const T & a, const T & b) { return a < b; }
#define ll long long
#define int ll
#define ull unsigned ll
#define _min(x, y) ((x)>(y)?(y):(x))
#define _max(x, y) ((x)>(y)?(x):(y))
#define max3(x, y, z) ( max( (x), max( (y), (z) ) ) )
#define min3(x, y, z) ( min( (x), min( (y), (z) ) ) )
#define pr(x, y) (make_pair((x), (y)))
#define pb(x) push_back(x)
using namespace std;
const int N = 1e5+5;
const int inf = 0x3f3f3f3f;
struct Node
{
int l, r;
double val;
}tr[N<<4];
int lisan[N];
int he[N];
int cnt;
inline void pushup(int p)
{
int lenl = lisan[tr[p<<1].r] - lisan[tr[p<<1].l];
int lenr = lisan[tr[p<<1|1].r] - lisan[tr[p<<1|1].l];
int lenm = lisan[tr[p<<1|1].l] - lisan[tr[p<<1].r];
double valm = 1.0 * (he[tr[p<<1|1].l] + he[tr[p<<1].r]) / 2;
double vall = tr[p<<1].val;
double valr = tr[p<<1|1].val;
tr[p].val = (1.0 * lenl * vall + lenr * valr + lenm * valm) / (lisan[tr[p].r] - lisan[tr[p].l]);
}
void build(int p, int l, int r)
{
tr[p].l = l; tr[p].r = r;
tr[p].val = 0;
if (l == r)
{
tr[p].val = he[l];
return;
}
int mid = (l + r) >> 1;
build(p<<1, l, mid);
build(p<<1|1, mid+1, r);
pushup(p);
}
Node ask(int p, int l, int r)
{
if (l >= r) return Node{l, r, 0};
if (l <= tr[p].l && tr[p].r <= r)
return tr[p];
int mid=(tr[p].l + tr[p].r) >> 1;
if (l > mid) return ask(p<<1|1, l, r);
else if (r <= mid) return ask(p<<1, l, r);
else
{
Node _l = ask(p<<1, l, r);
Node _r = ask(p<<1|1, l, r);
Node te;
te.l = _l.l; te.r = _r.r;
int lenl = lisan[_l.r] - lisan[_l.l];
int lenr = lisan[_r.r] - lisan[_r.l];
int lenm = lisan[_r.l] - lisan[_l.r];
double valm = 1.0 * (he[_r.l] + he[_l.r]) / 2;
double vall = _l.val;
double valr = _r.val;
te.val = (1.0 * lenl * vall + lenr * valr + lenm * valm) / (lisan[te.r] - lisan[te.l]);
return te;
}
}
int n, m;
double getn(long double mid, Node h)
{
long double r = mid, l = mid - m;
int pl = h.l; int pr = h.r;
if (pl > pr)
{
double hgl = 1.0 *(he[pl] - he[pl-1]) * (l - lisan[pl-1]) / (lisan[pl] - lisan[pl-1]) + he[pl-1];
double hgr = 1.0 *(he[pr+1] - he[pr]) * (r - lisan[pr]) / (lisan[pr+1] - lisan[pr]) + he[pr];
double val = (hgr + hgl) / 2;
return val / m;
}
double vall,valr;
if (pl == 1) vall = 0;
else
{
double hg = 1.0 *(he[pl] - he[pl-1]) * (l - lisan[pl-1]) / (lisan[pl] - lisan[pl-1]) + he[pl-1];
vall = (1.0 * hg + he[pl]) / 2;
}
if (pr == n) valr = 0;
else
{
double hg = 1.0 *(he[pr+1] - he[pr]) * (r - lisan[pr]) / (lisan[pr+1] - lisan[pr]) + he[pr];
valr = (1.0 * hg + he[pr]) / 2;
}
return (1.0 * h.val * (lisan[h.r] - lisan[h.l]) + vall * (lisan[h.l] - l) + valr * (r - lisan[h.r])) / m;
}
signed main()
{
scanf("%lld%lld", &n, &m);
for (int i = 1; i <= n; i++)
{
int te, y; scanf("%lld%lld", &te, &y);
lisan[++cnt] = te;
he[cnt] = y;
}
lisan[n+1] = 1ll * inf * inf;
build(1, 1, n);
int l = lisan[1];
int r = l - m;
double ans = 0;
while(r <= lisan[n])
{
int lr = upper_bound(lisan+1, lisan+n+1, l) - lisan;
int rc = upper_bound(lisan+1, lisan+n+1, r) - lisan;
int gg = min(lisan[lr] - l, lisan[rc] - r);
Node val = ask(1, rc, lr-1);
long double ml = l, mr = l + gg;
while(mr - ml > 1e-8)
{
long double mid = (mr + ml) / 2;
long double mmid = (mid + mr) / 2;
if (getn(mid, val) < getn(mmid, val)) ml = mid;
else mr = mmid;
}
ans = max(ans, getn(ml, val));
r += gg; l += gg;
}
printf("%.9f\n", ans);
return 0;
}