1.二分 NOIP2016 跳石头
//Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include<cmath> #include<algorithm> #include<cstring> using namespace std; const int maxn=50000+100; int l1,m,n,a[maxn],b[maxn],l,r,mid,ans,mxx=1e9; int check(int ans) { for(int i=1;i<=n;i++) b[i]=a[i]; int st=m; for(int i=1;i<=n;i++) { if(a[i]-b[i-1]<ans) { st--; if(st<0)return 0; b[i]=a[i-1]; } } return 1; } int main() { freopen("stone.in","r",stdin); freopen("stone.out","w",stdout); scanf("%d%d%d",&l1,&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]<mxx)mxx=a[i]; } a[0]=0; a[++n]=l1; l=mxx;r=l1; while(l<=r) { mid=(l+r)>>1; if(check(mid)){ans=mid;l=mid+1;} else r=mid-1; } printf("%d ",ans); return 0; }
2.洛谷三分模板
//Twenty #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<queue> #include<cmath> using namespace std; const double eps=1e-7; int n; double ans,a[20],l,r; double js(double x){ double base=1,ret=0; for(int i=n;i>=1;i--){ base*=x; ret+=base*a[i]; } return ret+a[n+1]; } int main() { scanf("%d%lf%lf",&n,&l,&r); for(int i=1;i<=n+1;i++) scanf("%lf",&a[i]); while(r-l>eps){ double mid1=l+(r-l)/3,mid2=r-(r-l)/3; if(js(mid1)>=js(mid2)) ans=mid1,r=mid2; else l=mid1; } printf("%.5lf",l); return 0; }