北大ACM1064——Cable master

题目主要的意思是:有N条绳子,它们的长度分别为Li,如果从它们中切出K条长度相同的绳子的话,这K条绳子每一条的最长的长度能是多少?

这一题用二分搜索可以很好的进行求解。

长度分别为0到INF(INF为尽可能大的数)。然后进行二分搜索,不断的缩小范围,循环一定的次数,可以很好的将答案求出。

G++double要用%lf输入,用%f输出,C++是window下的,用%lf输入,用%lf输出。

下面是AC的代码:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

const int INF = 10005;
const int MAX_N = 10005;
int N, K;
double Len[MAX_N];
bool C(double x)
{
	int num = 0;
	for(int i = 0; i < N; i++)
	{
		num += (int)(Len[i] / x);
	}
	return num >= K;
}

void solve()
{
	double lb = 0.0, ub = (double)INF * 10.0, mid;
	for(int i = 0; i < 100; i++)               //循环一百次,可以足够求解了
	{
		mid = (lb + ub) / 2;
		if(C(mid))
			lb = mid;
		else
			ub = mid;
	}
	printf("%.2f\n", floor(ub * 100) / 100);       //注意,这里需要用G++提交,如果是C++提交的话,改成%.2lf。
}

int main()
{
	int i;
	while(scanf("%d%d", &N, &K) != EOF)
	{
		for(i = 0; i < N; i++)
			scanf("%lf", &Len[i]);
		solve();
	}
	return 0;
}
时间: 2024-10-16 08:43:20

北大ACM1064——Cable master的相关文章

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; ++

【hoj】1604 cable master

简单,二分法,但是在oj上交的时候会有精度的限制,所以只能把数扩得大一点,而且在扩的时候为防止尾数会自动生成一些非零数,所以还是自己吧扩到的位置设置为0,还有输出时因为%.2lf会自己有4设5入,所以可以自己算出小数点后两位的数值,在输出,保证要求的精度 #include <iostream> #include <stdio.h> #include <algorithm> #define MAX 10010 using namespace std; long long