Codeforces 1011C Fly(二分+模拟)

题意:

是有n个星球,1代表地球,然后输入一个m表示火箭的重量,然后输入了两组n个数,第一组表示在每个星球起飞时消耗一吨燃料的质量数,a[i]就表示每a[i]吨就要消耗1吨燃料,第二组就表示在每个星球降落时消耗一吨燃料的质量数,然后问当火箭从1飞到2到3....到n星球后又返回1星球最少需要加多少燃料。

思路:

二分答案,注意double二分的写法,以及如何控制出口

代码:

#include<iostream>
#include<iomanip> 
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional>
#include<list>
    
#define fst first
#define sc second
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) 
#pragma Gcc optimize(2)

using namespace std;

typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL;


const int maxn = 1000 + 100;
const int maxm = 5e3 + 100;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
int scan(){
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')
        res=res*10+ch-'0';
    return flag?-res:res;
}

double a[maxn], b[maxn];
int n, m;
bool ok(double ans){
    for(int i = 1; i <= n; i++){
        ans -= (m+ans)/a[i];
        ans -= (m+ans)/b[i+1];
    }
    if(ans < 0) return false;
    else return true;
}
int main(){
    scanf("%d",&n);
    scanf("%d", &m);
    
    for(int i = 1; i <= n; i++){
        scanf("%lf", &a[i]);
        if(a[i] <= 1){
            printf("-1");
            return 0;
        }
    }
    for(int i = 1; i <= n; i++){
        scanf("%lf", &b[i]);
        if(b[i] <= 1){
            printf("-1");
            return 0;
        }
    }b[n+1] = b[1];
    double ans = 1;
    double l = 0, r = 10000000000;
    for(int i = 1; i <= 1000000 ; i++){
        if(r-l < 1e-9) break;
        double mid = (l+r)/2.0;
        if(ok(mid))r = mid;
        else l = mid;
    }
    printf("%.10lf", l);
    return 0;
}
/*
*/

猜你喜欢

转载自www.cnblogs.com/wrjlinkkkkkk/p/9470512.html