牛客练习赛72 C. brz的序列

链接

https://ac.nowcoder.com/acm/contest/8282/C

题意

一个长度为 n n n 的序列,定义一次操作要选定一个 i ∈ ( 1 , n ) i\in(1,n) i(1,n),然后将序列中第 i i i 个数变成与它相邻的两个数的平均数。

在可以进行无限次任意位置的操作的情况下,求能得到的序列最小总和。

思路

i i i 看作平面直角坐标系上的一个点 ( i , a i ) (i,a_i) (i,ai)

在操作完后,所有的点会变成一个下凸壳的形状。

因此,只要求出下凸壳,两点之间对纵坐标求等差数列和就行。

代码

#include <bits/stdc++.h>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef double DB;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
//head
const double EPS=1e-8;
const int N=1e6+5;
int n,top;
int sgn(double x) {return fabs(x)<EPS?0:(x>0?1:-1);}
struct P {
    double x,y;
    P() {}
    P(double x,double y):x(x),y(y) {}

    bool operator == (const P &a) const {return !sgn(x-a.x)&&!sgn(y-a.y);}
    bool operator < (const P &a) const {return sgn(x-a.x)<0||sgn(x-a.x)==0&&sgn(y-a.y)<0;}

    P operator - (const P &a) const {return P(x-a.x,y-a.y);}

    double operator ^ (const P &a) const {return x*a.y-y*a.x;}  // 叉积

    double dist(P p) {return hypot(x-p.x,y-p.y);}  // 两点距离
}p[N],stk[N];
int main() {
    //freopen("E:/OneDrive/IO/in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        DB y;
        scanf("%lf",&y);
        p[i]=P(i,y);
    }
    sort(p+1,p+1+n);
    for(int i=1;i<=min(n,2);i++) stk[++top]=p[i];
    for(int i=3;i<=n;i++) {
        while(top>=2&&sgn((stk[top]-stk[top-1])^(p[i]-stk[top]))<=0) --top;
        stk[++top]=p[i];
    }
    double res=stk[1].y;
    for(int i=2;i<=top;i++) {
        double d=(stk[i].y-stk[i-1].y)/(stk[i].x-stk[i-1].x);
        res+=(stk[i].x-stk[i-1].x)*(stk[i-1].y+d)+(stk[i].x-stk[i-1].x)*(stk[i].x-stk[i-1].x-1)*d/2;
    }
    printf("%.10f\n",res);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值