• [USACO14MAR]破坏Sabotage 二分答案


    题目描述

    Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's milking equipment!

    The milking equipment consists of a row of N (3 <= N <= 100,000) milking machines, where the ith machine produces M_i units of milk (1 <= M_i <= 10,000). Farmer Paul plans to disconnect a contiguous block of these machines -- from the ith machine up to the jth machine (2 <= i <= j <= N-1); note that Farmer Paul does not want to disconnect either the first or the last machine, since this will make his plot too easy to discover. Farmer Paul's goal is to minimize the average milk production of the remaining machines. Farmer Paul plans to remove at least 1 cow, even if it would be better for him to avoid sabotage entirely.

    Fortunately, Farmer John has learned of Farmer Paul's evil plot, and he is wondering how bad his milk production will suffer if the plot succeeds. Please help Farmer John figure out the minimum average milk production of the remaining machines if Farmer Paul does succeed.

    农夫约翰的头号敌人保罗决定破坏农民约翰的挤奶设备。挤奶设备排成一行,共N(3<= N <=100000)台挤奶机,其中第i个台挤奶机生产M_i单位(1 <= M_i<=10,000)的牛奶。

    保罗计划切断一段连续的挤奶机,从第i台挤奶机到第j台挤奶机(2<= i<= j<= N-1)。注意,他不希望断开第一台或最后一台挤奶机,因为这将会使他的计划太容易被发现。保罗的目标是让其余机器的平均产奶量最小。保罗计划除去至少1台挤奶机。

    请计算剩余机器的最小平均产奶量。

    输入输出格式

    输入格式:

    第 1 行:一个整数 N。

    第 2 到 N+1 行:第 i+1 行包含一个整数 M_i。

    输出格式:

    第 1 行: 一个实数, 表示平均牛奶产量的最小值, 保留三位小数 (四舍五入)。

    输入输出样例

    输入样例#1: 复制
    5
    5
    1
    7
    8
    2
    输出样例#1: 复制
    2.667

    说明

    【样例说明】

    移去 7 和 8,剩下 5, 1, 2,平均值为 8/3。

    【数据规模和约定】

    对于 30%的数据,N <= 1,000。

    对于 50%的数据,N <= 10,000。

    对于 100%的数据,3 <= N <= 100,000,1 <= M_i <= 10,000。

    【时空限制】

    0.2s/128M

    前缀和很容易想到;

    然后推一推式子,二分答案看是否可行;

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstdlib>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    #include<bitset>
    #include<ctime>
    #include<deque>
    #include<stack>
    #include<functional>
    #include<sstream>
    //#include<cctype>
    //#pragma GCC optimize(2)
    using namespace std;
    #define maxn 200005
    #define inf 0x7fffffff
    //#define INF 1e18
    #define rdint(x) scanf("%d",&x)
    #define rdllt(x) scanf("%lld",&x)
    #define rdult(x) scanf("%lu",&x)
    #define rdlf(x) scanf("%lf",&x)
    #define rdstr(x) scanf("%s",x)
    typedef long long  ll;
    typedef unsigned long long ull;
    typedef unsigned int U;
    #define ms(x) memset((x),0,sizeof(x))
    const long long int mod = 1e9 + 7;
    #define Mod 1000000000
    #define sq(x) (x)*(x)
    #define eps 1e-3
    typedef pair<int, int> pii;
    #define pi acos(-1.0)
    //const int N = 1005;
    #define REP(i,n) for(int i=0;i<(n);i++)
    typedef pair<int, int> pii;
    inline ll rd() {
        ll x = 0;
        char c = getchar();
        bool f = false;
        while (!isdigit(c)) {
            if (c == '-') f = true;
            c = getchar();
        }
        while (isdigit(c)) {
            x = (x << 1) + (x << 3) + (c ^ 48);
            c = getchar();
        }
        return f ? -x : x;
    }
    
    ll gcd(ll a, ll b) {
        return b == 0 ? a : gcd(b, a%b);
    }
    int sqr(int x) { return x * x; }
    
    
    /*ll ans;
    ll exgcd(ll a, ll b, ll &x, ll &y) {
        if (!b) {
            x = 1; y = 0; return a;
        }
        ans = exgcd(b, a%b, x, y);
        ll t = x; x = y; y = t - a / b * y;
        return ans;
    }
    */
    
    int n;
    int a[maxn];
    int sum[maxn];
    double ans = inf * 1.0;
    bool check(double k) {
        double minn = sum[1] - k * 1.0;
        for (int i = 2; i < n; i++) {
            if ((sum[n] - k * n - (sum[i] - k * i) + minn) <= 0)return true;
            minn = min(minn, sum[i] - k * i);
        }
        return false;
    }
    
    int main() {
        //ios::sync_with_stdio(0);
        rdint(n);
        for (int i = 1; i <= n; i++)rdint(a[i]);
        
        for (int i = 1; i <= n; i++) {
            sum[i] = sum[i - 1] + a[i];
        }
        double l = 0, r = 1e7 * 1.0;
        while (r - l > 1e-5) {
            double mid = (l + r) / 2.0;
            if (check(mid))r = mid;
            else l = mid;
        }
        printf("%.3lf
    ", 1.0*l);
        return 0;
    }
    
    EPFL - Fighting
  • 相关阅读:
    【server】瞎写
    【windows c++ 笔记】随笔
    【VScode】使用VScode来写markdown ② 流程图
    【VScode】使用VScode 来写markdown ① 时序图
    【windows c++ 笔记】 正则表达
    【sublime text】中文乱码问题解决过程
    【vue3】从0搭建Vue3项目
    【git】设置VScode为默认查看差异工具
    【lua实战摸索】C API&auxiliary library 使用笔记
    【vue3】监控响应: reactive/watch/watchEffect/provide.inject/computed
  • 原文地址:https://www.cnblogs.com/zxyqzy/p/10257119.html
Copyright © 2020-2023  润新知