【Codeforces 578 C. Weakness and Poorness】三分+最大子段和

博客详细介绍了如何解决Codeforces上的题目578C,该题涉及序列的贫乏度和弱度概念。弱度是指序列中所有区间最大贫乏度的值,目标是找到一个x值,使得序列减去x后的弱度最小。博主通过观察得出这是一个谷函数,并采用三分搜索策略配合最大子段和算法来求解,特别地,通过取数组的相反数来处理绝对值情况。在实现过程中,由于精度问题导致超时,最终将精度调整为3e-12后成功通过测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cf578C

• 给定一个序列A
• 一个区间的poorness定义为这个区间内和的绝对值
• weakness等于所有区间最大的poorness
• 求一个x使得,序列A全部减x后weakness最小
• 1 ≤ n ≤ 2 * 1e5

做法 首先对样例发现随着x增加 答案会增加 x减少 答案会减少 那么这是一个谷函数 可以三分
最大子段和就用最大子段和的算法算 但是绝对值 只要把数组相反数再求一次就是绝对值的情况了
eps 1e-13 TLE一发 3e-12过的

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);

/*namespace sgt
{
    #define mid ((l+r)>>1)

    #undef mid
}*/
const int MAX_N = 200025;
double arr[MAX_N],b[MAX_N];
int n;
double cal(double x,double arr[])
{
    for(int i = 1;i<=n;++i)
        b[i]= arr[i]-x;
    double tmp = 0,ans = 0;
    for(int i = 1;i<=n;++i)
    {
        if(tmp+b[i]<=0) tmp = 0.0;
        else tmp+=b[i];
        ans = max(ans,tmp);
    }
    for(int i = 1;i<=n;++i)
        b[i]=-b[i];
    tmp = 0.0;
    for(int i = 1;i<=n;++i)
    {
        if(tmp+b[i]<=0) tmp = 0.0;
        else tmp+=b[i];
        ans = max(ans,tmp);
    }
    return ans;
}
double trisection_search(double left,double right)
{
    double midl,midr;
    while(right-left>3e-12)
    {
        midl = (left*2+right)/3.0;
        midr = (left+right*2)/3.0;
        if(cal(midl,arr)<=cal(midr,arr)) right = midr;
        else left = midl;
    }
    return cal(left,arr);
}
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    scanf("%d",&n);
    for(int i = 1;i<=n;++i) scanf("%lf",&arr[i]);
//    dbg(cal(2,arr));
    printf("%.12f\n",trisection_search(-10001,10001));
//    for(int i = 1;i<=n;++i) dbg2(i,arr[i]);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值