A - Cable master (HDU - 1551)

- 题目大意

有n条绳子,分成k段相等的,问能使得最长为多长。

- 解题思路

采用二分法一直逼近求极限状态(注意精度!!!)。

- 代码

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int n, k;
double num[10001];
bool find(double x)
{
	int qum = 0;
	for (int i = 0; i < n; i++)
		qum += (int)(num[i] / x);
	if (qum >= k)
		return true;
	return false;
}
int main()
{
	double sum;
	while (cin >> n >> k)
	{
		if (n == 0 && k == 0)
			break;
		sum = 0;
		double max = 0;
		for (int i = 0; i < n; i++)
		{
			cin >> num[i];
			if (num[i] > max)
				max = num[i];
		}

		double l = 0, r = max;
		double m;
		for(int i=1;i<100;i++)
		{
			m = (l + r) / 2;
			if (find(m))
				l = m;
			else
			{
				r = m;
			}
		}
		cout <<fixed<<setprecision(2)<< l<<endl;
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/alpacadh/p/8448871.html

时间: 2024-10-08 07:12:55

A - Cable master (HDU - 1551)的相关文章

Cable master HDU - 1551 —— 二分基础模板(精度问题)

Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to organize the most honest contest ever. It was decided to connect computers for the contestants using a "star&

hdu 1551 Cable master(二分)

Cable master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2003    Accepted Submission(s): 751 Problem Description Inhabitants of the Wonderland have decided to hold a regional programming co

POJ1064 Cable master 【精度问题】

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24897   Accepted: 5339 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to

POJ 1064 Cable master(很好玩的二分搜索)

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24292   Accepted: 5200 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to

POJ 1064 Cable master 浮点数二分

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21181   Accepted: 4571 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to

poj1064 Cable master

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25643   Accepted: 5504 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to

(poj)1064 Cable master 二分+精度

题目链接:http://poj.org/problem?id=1064 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to organize the most honest contest ever. It was decided to conn

POJ 1064 Cable master (二分 分数化整数)

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28764   Accepted: 6091 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to

poj 1064 Cable master ,二分 精度!!!

给出n根绳子,求把它们切割成K条等长的绳子的最大长度是多少? 二分 用 for(int i=0; i<100; ++i) 代替   while(r-l>eps) 循环100次精度能达到1e-30,基本上能一般题目的精度要求. 而 浮点数二分区间的话容易产生精度缺失导致死循环. #include<cstdio> double L[10000 + 10]; int n, k; int ok(double x) { int cnt = 0; for(int i=0; i<n; ++