【Codeforces 578 C. Weakness and Poorness】三分+最大子段和
作者:互联网
• 给定一个序列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;
}
标签:578,const,子段,int,Weakness,ll,double,include,mod 来源: https://blog.csdn.net/heucodesong/article/details/95867672