結果

問題 No.67 よくある棒を切る問題 (1)
ユーザー SlephySlephy
提出日時 2022-05-06 18:35:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 185 ms / 5,000 ms
コード長 1,922 bytes
コンパイル時間 2,043 ms
コンパイル使用メモリ 197,388 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-26 01:34:06
合計ジャッジ時間 7,198 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 150 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 70 ms
6,940 KB
testcase_03 AC 129 ms
6,944 KB
testcase_04 AC 163 ms
6,944 KB
testcase_05 AC 167 ms
6,944 KB
testcase_06 AC 155 ms
6,944 KB
testcase_07 AC 179 ms
6,944 KB
testcase_08 AC 185 ms
6,944 KB
testcase_09 AC 173 ms
6,940 KB
testcase_10 AC 148 ms
6,944 KB
testcase_11 AC 160 ms
6,940 KB
testcase_12 AC 138 ms
6,940 KB
testcase_13 AC 168 ms
6,944 KB
testcase_14 AC 174 ms
6,944 KB
testcase_15 AC 149 ms
6,944 KB
testcase_16 AC 148 ms
6,940 KB
testcase_17 AC 141 ms
6,940 KB
testcase_18 AC 132 ms
6,940 KB
testcase_19 AC 159 ms
6,940 KB
testcase_20 AC 159 ms
6,940 KB
testcase_21 AC 153 ms
6,940 KB
testcase_22 AC 119 ms
6,940 KB
testcase_23 AC 127 ms
6,944 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 5 ms
6,944 KB
testcase_26 AC 3 ms
6,944 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 30 ms
6,940 KB
testcase_29 AC 16 ms
6,940 KB
testcase_30 AC 5 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://yukicoder.me/submissions/758359
// 相対誤差に応じて二分探索を終了できるように改良
// とりあえず、doulbe 型のみの対応
// もっと整理しました
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = (int)1e9 + 1001010;
const ll llINF = (ll)4e18 + 11000010;
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
ll ceil(ll a, ll b){return (a+b-1) / b;};
// ================================== ここまでテンプレ ==================================

// とある条件を満たす区間の境界を見つける
// 探索区間は [ok, ng) または (ng, ok]
// ok はつねに「とある条件」を満たす
// ng はつねに「とある条件」を満たさない
// 「とある条件」を満たすかどうかは、judge関数によって求められる
template<class Judgement>
double Binary_Search_double(double ok, double ng, Judgement judge, double tolerance, bool considerRelativeError = false){
    auto need_continue = [&]() -> bool{
        if((fabs(ok - ng) < tolerance)) return false; // 絶対誤差
        if(considerRelativeError && (fabs(ok - ng) < tolerance * fabs((ok + ng) * 0.5))) return false; // 相対誤差
        return true;
    };
    
    while(need_continue()){
        double mid = (ok + ng) * 0.5;
        if(judge(mid)) ok = mid;
        else ng = mid;
    }
    return ok;
};

int main(){
    int n; cin >> n;
    vector<double> l(n);
    for(int i = 0; i < n; i++) cin >> l[i];
    ll k; cin >> k;

    auto judge = [&](double mid) -> bool{
        if(mid <= 0) return true; // ここ大事
        ll count = 0;
        for(int i = 0; i < n; i++){
            count += floor(l[i] / mid);
            if(count >= k) return true;
        }
        return false;
    };

    double ans = Binary_Search_double(-1.0, 1e12, judge, 1e-10, true);
    printf("%.12lf\n", ans);
    return 0;
}
0