暑期集训2:ACM基础算法 例1:POJ-1064

news/2024/7/5 2:19:41

2018学校暑期集训第二天——ACM基础算法

例一  ——  POJ - 1064 

Cable master

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" topology - i.e. connect them all to a single central hub. To organize a truly honest contest, the Head of the Judging Committee has decreed to place all contestants evenly around the hub on an equal distance from it. 
To buy network cables, the Judging Committee has contacted a local network solutions provider with a request to sell for them a specified number of cables with equal lengths. The Judging Committee wants the cables to be as long as possible to sit contestants as far from each other as possible. 
The Cable Master of the company was assigned to the task. He knows the length of each cable in the stock up to a centimeter,and he can cut them with a centimeter precision being told the length of the pieces he must cut. However, this time, the length is not known and the Cable Master is completely puzzled. 
You are to help the Cable Master, by writing a program that will determine the maximal possible length of a cable piece that can be cut from the cables in the stock, to get the specified number of pieces.

Input

The first line of the input file contains two integer numb ers N and K, separated by a space. N (1 = N = 10000) is the number of cables in the stock, and K (1 = K = 10000) is the number of requested pieces. The first line is followed by N lines with one number per line, that specify the length of each cable in the stock in meters. All cables are at least 1 meter and at most 100 kilometers in length. All lengths in the input file are written with a centimeter precision, with exactly two digits after a decimal point.

Output

Write to the output file the maximal length (in meters) of the pieces that Cable Master may cut from the cables in the stock to get the requested number of pieces. The number must be written with a centimeter precision, with exactly two digits after a decimal point. 
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output file must contain the single number "0.00" (without quotes).

Sample Input

4 11
8.02
7.43
4.57
5.39

Sample Output

2.00

    这道题属于二分查找题型

题目大意:有N条绳子,他们的长度分别为ai,如果从它们中切割出k条长度相同的绳子的话,这K条绳子每条能有多长?

假定一个解然后判断是否可行

也就是求一个x ,   a1/ x +a2/x +a3/x +.....=K,求最大的x。求的过程中中间值x ,如果>=k也时 ,要求最大的x.


#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
using namespace std;
const int maxn = 10010;int n, k;
double a[maxn];bool judge(double x)
{int num = 0;for(int i = 0; i < n; i++)num += (int)(a[i] / x);return num >= k;
}int main(void)
{while(~scanf("%d%d", &n, &k)){for(int i = 0; i < n; i++)scanf("%lf", &a[i]);double left = 0, right = maxn * 10;for(int i = 0; i < 1000; i++){double mid = (left + right) / 2;if(judge(mid))left = mid;elseright = mid;}printf("%0.2f\n", floor(right*100)/100);}return 0;
}

 


http://lihuaxi.xjx100.cn/news/247411.html

相关文章

实验: VMware使用快照间接备份原始VMDK文件

资料上看的使用快照备份运行着的虚拟机 当虚拟机开着时&#xff0c;快照提供了一个备份原始 VMDK 文件的好办法。所有的写入操作在原始文件上暂停了&#xff0c;因此&#xff0c;复制它在另一个存储卷很安全。这就是像 VMware Consolidated Backup 和 Vizioncore 的 vRanger…

Linux学习笔记8——bash基本概念

一个操作系统的组成中&#xff0c;shell是与用户最接近的部分shell&#xff1a;外壳&#xff0c;也是一种程序GUI&#xff1a;Gnome&#xff0c;KDE,XfaceCLI&#xff1a;sh&#xff0c;csh&#xff0c;ksh&#xff0c;bash&#xff0c;tcsh&#xff0c;zshLinux中大多使用bash…

Latex公式

1. LaTex的数学公式基本规则 1.1. 转义 以下几个字符: # $ % & ~ _ ^ \ { }有特殊意义&#xff0c;需要表示这些字符时&#xff0c;需要转义&#xff0c;即在每个字符前加上\&#xff08;转义字符的具体含义下面会解释&#xff09; \boxed命令给公式加一个方框。\fbox具有…

暑期集训2:ACM基础算法 例2:POJ-2456

2018学校暑期集训第二天——ACM基础算法 例二 —— POJ - 2456 Aggressive cows Farmer John has built a new long barn, with N (2 < N < 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 < xi < 1,000,000,000…

Linux启动流程(二)

//...根据grub内核映像所在路径,读取内核映像&#xff0c;并进行解压缩操作。并调用start_kernel()函数来启动一系列的初始化函数并初始化各种设备&#xff0c;完成Linux核心环境的建立1.start_kernel(init/main.c)中调用一系列初始化函数:(1) 在屏幕上打印出当前的内核版本信息…

TL-WDN3321 Ubuntu 下安装

为什么80%的码农都做不了架构师&#xff1f;>>> The WiFi USB dongles based on the newest RT5572 chip set do not work out of the box on Ubuntu. Unex DNUR-V72, D-Link DWA-160 Rev B and TP-Link TL-WDN3200 dongles are based on this chipset. You will …

C++数值极限numeric_limits

一般来说&#xff0c;数值类型的极值是一个与平台相关的特性。C标准程序库通过template numeric_limits提供这些极值&#xff0c;取代传统C语言所采用的预处理常数。你仍然可以使用后者&#xff0c;其中整数常数定义于<climits>和<limits.h>,浮点常数定义于<cfl…

[异常笔记] spring boot 启动-2018040201

异常 1、编码引发异常 00:59:49.311 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : [] 00:59:49.318 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for re…