Cable master
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 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.
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).
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、k,n是n和木棍,问你能不能从这n个棍中截取出k个等长的木棍,输出最大的木棍长度,保留二位小数。
思路:二分来做,题目中木棍最长为100000.故可以二分0~100000.1,找出满足条件的最大长度,小数二分需要注意精度,
就用while循环重复一千次,得到满足条件的解,具体的看代码中的解释。
1 #include <map> 2 #include <set> 3 #include <list> 4 #include <stack> 5 #include <queue> 6 #include <deque> 7 #include <cmath> 8 #include <ctime> 9 #include <string> 10 #include <limits> 11 #include <cstdio> 12 #include <vector> 13 #include <iomanip> 14 #include <cstdlib> 15 #include <cstring> 16 #include <istream> 17 #include <iostream> 18 #include <algorithm> 19 #define ci cin 20 #define co cout 21 #define el endl 22 #define Scc(c) scanf("%c",&c) 23 #define Scs(s) scanf("%s",s) 24 #define Sci(x) scanf("%d",&x) 25 #define Sci2(x, y) scanf("%d%d",&x,&y) 26 #define Sci3(x, y, z) scanf("%d%d%d",&x,&y,&z) 27 #define Scl(x) scanf("%I64d",&x) 28 #define Scl2(x, y) scanf("%I64d%I64d",&x,&y) 29 #define Scl3(x, y, z) scanf("%I64d%I64d%I64d",&x,&y,&z) 30 #define Pri(x) printf("%d\n",x) 31 #define Prl(x) printf("%I64d\n",x) 32 #define Prc(c) printf("%c\n",c) 33 #define Prs(s) printf("%s\n",s) 34 #define For(i,x,y) for(int i=x;i<y;i++) 35 #define For_(i,x,y) for(int i=x;i<=y;i++) 36 #define FFor(i,x,y) for(int i=x;i>y;i--) 37 #define FFor_(i,x,y) for(int i=x;i>=y;i--) 38 #define Mem(f, x) memset(f,x,sizeof(f)) 39 #define LL long long 40 #define ULL unsigned long long 41 #define MAXSIZE 10006 42 #define INF 0x3f3f3f3f 43 44 const int mod=1e9+7; 45 const double PI = acos(-1.0); 46 47 using namespace std; 48 49 double a[MAXSIZE]; 50 int n,k; 51 52 int ccount(double x) 53 { 54 int sum=0; 55 For(i,0,n) 56 sum+=(int)(a[i]/x);//这里计算满足条件的根数总数、向下取整 57 return sum>=k;//只要总数大于等于k即可 58 } 59 int main() 60 { 61 while(~Sci2(n,k)) 62 { 63 For(i,0,n) 64 scanf("%lf",&a[i]); 65 double l=0,r=100000.1; 66 int cnt=1000; 67 while(cnt--)//重复一千次,是的l~r之间无限小,满足之间的所有数都可以满足题目条件 68 { 69 double mid=(l+r)/2; 70 if(ccount(mid)) 71 l=mid; 72 else 73 r=mid; 74 } 75 printf("%.2f\n",floor(r*100)/100);//注意这里,%.2f是自动四舍五入到的,所以这里先乘100在向下取整,再除以一百保证截取前三位 76 } 77 return 0; 78 }