結果

問題 No.67 よくある棒を切る問題 (1)
ユーザー cielciel
提出日時 2018-06-11 19:01:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 113 ms / 5,000 ms
コード長 1,537 bytes
コンパイル時間 542 ms
コンパイル使用メモリ 69,260 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-26 00:29:50
合計ジャッジ時間 4,250 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 43 ms
6,944 KB
testcase_03 AC 81 ms
6,940 KB
testcase_04 AC 106 ms
6,944 KB
testcase_05 AC 109 ms
6,940 KB
testcase_06 AC 109 ms
6,940 KB
testcase_07 AC 109 ms
6,944 KB
testcase_08 AC 110 ms
6,940 KB
testcase_09 AC 110 ms
6,940 KB
testcase_10 AC 97 ms
6,944 KB
testcase_11 AC 103 ms
6,944 KB
testcase_12 AC 94 ms
6,940 KB
testcase_13 AC 101 ms
6,940 KB
testcase_14 AC 104 ms
6,940 KB
testcase_15 AC 93 ms
6,940 KB
testcase_16 AC 110 ms
6,944 KB
testcase_17 AC 109 ms
6,940 KB
testcase_18 AC 110 ms
6,940 KB
testcase_19 AC 110 ms
6,944 KB
testcase_20 AC 111 ms
6,944 KB
testcase_21 AC 110 ms
6,940 KB
testcase_22 AC 99 ms
6,940 KB
testcase_23 AC 105 ms
6,940 KB
testcase_24 AC 1 ms
6,948 KB
testcase_25 AC 4 ms
6,940 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 20 ms
6,940 KB
testcase_29 AC 10 ms
6,944 KB
testcase_30 AC 4 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <cstdio>

// cpp_binarysearch (C) @cielavenir under Boost Software License.
// https://github.com/cielavenir/cpp_libraries/tree/master/binarysearch
// returns the smallest value satisfying the predicate.

#include <functional>
template<typename T,typename F=std::function<bool(T)>>
T binarysearch(T lo,T hi,const T eps,const F &predicate){
	T r=hi+eps;
	for(;lo<hi+eps;){
		T mi=eps!=1||(lo<0)==(hi<0) ? lo+(hi-lo)/2 : (lo<-hi) ? -((-lo-hi-1)/2+1) : (lo+hi)/2;
		if(predicate(mi)){
			r=mi;
			hi=mi-eps;
		}else{
			lo=mi+eps;
		}
	}
	return r;
}
template<typename T,typename F=std::function<bool(T)>>
T binarysearch(T lo,T hi,const F &predicate){return binarysearch(lo,hi,(T)1,predicate);}

template<typename F=std::function<bool(double)>>
double binarysearch(double _lo,double _hi,const F &predicate){
	long long lo;
	if(_lo<0)_lo=-_lo,lo=-*(long long*)(&_lo);else lo=*(long long*)(&_lo);
	long long hi;
	if(_hi<0)_hi=-_hi,hi=-*(long long*)(&_hi);else hi=*(long long*)(&_hi);
	long long _r=binarysearch(lo,hi,1LL,[&](long long _mi){
		double mi;
		if(_mi<0)_mi=-_mi,mi=-*(double*)(&_mi);else mi=*(double*)(&_mi);
		return predicate(mi);
	});
	double r;
	if(_r<0)_r=-_r,r=-*(double*)(&_r);else r=*(double*)(&_r);
	return r;
}

int main(){
	int n;
	long long k;
	scanf("%d",&n);
	std::vector<double>v(n);
	for(int i=0;i<n;i++){
		scanf("%lf",&v[i]);
	}
	scanf("%lld",&k);
	printf("%.9f\n",binarysearch(1e-9,1e9,[&](double h){
		long long _n=0;
		for(int i=0;i<n;i++){
			_n+=v[i]/h;
		}
		return _n<k;
	}));
}
0