P1487 失落的成绩单

P1487 失落的成绩单
a[i]=a[i-2]-2.0*a[i-1]+2.0*d;
a[2]越大,a[3]越小
a[3]越大,a[4]越小
所以a[2]越大,a[4]越大,a[3]越小
就有了单调性,分奇偶进行二分
细节:二分的时候,l不一定为0,1e10为浮点数,eps小点好

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(int i=a;i<=b;++i)
//by war
//2019.9.4
using namespace std;
int n,m;
long double d,a[N],l,r,mid,eps=1e-12,t;
void in(int &x){
    int y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(int x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

bool check(long double x){
    For(i,3,n) a[i]=a[i-2]-2.0*a[i-1]+2.0*d;
    return a[n]-t>=eps;
}

signed main(){
    in(n);in(m);
    cin>>d>>a[1]>>t;
    l=-1e12;r=1e12;
    while(r-l>eps){
        a[2]=(l+r)/2.0;
        if(check(a[2])){
            if(n&1) l=a[2];
            else r=a[2];
        }
        else{
            if(n&1) r=a[2];
            else l=a[2];
        }
    }
    printf("%.3Lf\n",a[m]);
    return 0;
}

 

posted @ 2019-09-04 17:18  WeiAR  阅读(173)  评论(0编辑  收藏  举报