Access Points (单调栈)

16 篇文章 0 订阅

题目大意:

平面上 n n n个点对序列 a a a,再选取 n n n个点对序列 b b b,使得在满足: i < j , x i ≤ x j , y i ≤ y j i<j,x_i \le x_j,y_i \le y_j i<j,xixj,yiyj情况下, ∑ i = 1 n ( a [ i ] . x − b [ i ] . x ) 2 + ( a [ i ] . y − b [ i ] . y ) 2 \sum_{i=1}^{n} (a[i].x-b[i].x)^2+(a[i].y-b[i].y)^2 i=1n(a[i].xb[i].x)2+(a[i].yb[i].y)2最小

解题思路:

  • x x x y y y可以分开来处理
  • 有一点是要清楚的是: ∑ k = l r ( x − a k ) 2 \sum_{k=l}^{r}(x-a_k)^2 k=lr(xak)2最小值,当 x = 1 i − j + 1 ∑ k = j i a k x=\frac{1}{i-j+1}\sum_{k=j}^ia_k x=ij+11k=jiak取到
  • 依据此维护一个单调栈,当一个块均值大于栈顶块的均值时,合并这两个块,直到栈里面的均值是单调的

AC代码:

#include <bits/stdc++.h>
#define ft first
#define sd second
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define seteps(N) fixed << setprecision(N)
#define endl "\n"
const int maxn = 1e5 + 10;
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll, int> pli;
const ll mod = 1e9 + 7;
int n;
int x[maxn], y[maxn];
db ans;
db sqr(db x) {return x * x;}
stack <pli> sta;
void solve(int *a) {
    for (int i = 1; i <= n; i++) {
        pli res = {a[i], 1};
        while (!sta.empty() && sta.top().ft * res.sd >= sta.top().sd * res.ft) {
            res.ft += sta.top().ft;
            res.sd += sta.top().sd;
            sta.pop();
        }
        sta.push(res);
    }
    int r = n, l;
    while (!sta.empty()) {
        pli res = sta.top(); sta.pop();
        l = r - res.sd + 1;
        for (int i = l; i <= r; i++) 
            ans += sqr(a[i] - 1.0 * res.ft / res.sd);
        r = l - 1;
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> x[i] >> y[i];
    solve(x), solve(y);
    cout << seteps(12) << ans << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值