結果

問題 No.67 よくある棒を切る問題 (1)
ユーザー ciel
提出日時 2018-06-11 19:00:01
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 113 ms / 5,000 ms
コード長 1,541 bytes
コンパイル時間 666 ms
コンパイル使用メモリ 64,792 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2025-03-03 11:14:15
合計ジャッジ時間 4,345 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:45:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   45 |         scanf("%d",&n);
      |         ~~~~~^~~~~~~~~
main.cpp:48:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   48 |                 scanf("%lf",&v[i]);
      |                 ~~~~~^~~~~~~~~~~~~
main.cpp:50:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   50 |         scanf("%lld",&k);
      |         ~~~~~^~~~~~~~~~~

ソースコード

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(1.0e-9,1.0e9,[&](double h){
		long long _n=0;
		for(int i=0;i<n;i++){
			_n+=v[i]/h;
		}
		return _n<k;
	}));
}
0