题解:P16578 [GKS 2016 #A] Jane's Flower Shop
int_long_double · · 题解
题面简化:
由题可知,此题就是要用二分来求解。其中,通过数据范围可以得知,这个二分需要不断接近于零。并且这个二分需要在 1 与 -1 之间进行。然而我们要设计一个 check 函数来作为二分检测。
检查函数
检查函数中我们采用秦九韶算法来计算。当然了要保证精度,因此我们使用的是浮点数类型。在相乘的过程中还需要用小数相乘保证精度。
注意:
二分时需要注意精度误差,例如在二分之中的循环要用一个精度极高的数来判断。
代码:
#include<iostream>
#include<iomanip>
#include<cstdio>
using namespace std;//高精度数(INF_MIN)
#define INF_MIN 1e-10
#define N 105
#define ld long double
#define ll long long
#define in cin>>
#define out cout<<
#define l <<
#define r >>
#define endl printf("\n");
int n;
ld a[N];
bool check(ld x){//检测
ld sum=0;
for(int i=0;i<=n;i++) sum=sum*(1.0L+x)+a[i];
return sum>=0;//不是负数
}
int main(){
ll T;in T;//读入
ll x=0;
while(T--){//T次
x++;
out "Case #" l x l ": ";//输出
in n r a[0];a[0]=-a[0];//转负
for(int i=1;i<=n;i++) in a[i];//读入
ld leftt=-1,rightt=1,mid;
if(check(rightt)){//检查
while(rightt-leftt>INF_MIN){//方案一
mid=(leftt+rightt)/2;
if(check(mid)){//Check
rightt=mid;
}else{
leftt=mid;
}
}
}else{
while(rightt-leftt>INF_MIN){//方案二
mid=(leftt+rightt)/2;
if(check(mid)){//Check
leftt=mid;
}else{
rightt=mid;
}
}
}
out fixed l setprecision(15) l leftt;
endl
}
return 0;
}
个人码风,不喜勿喷。