首页 > 代码库 > POJ 1064 Cable master (二分)

POJ 1064 Cable master (二分)

题意:给定 n 条绳子,它们的长度分别为 ai,现在要从这些绳子中切出 m 条长度相同的绳子,求最长是多少。

析:其中就是一个二分的水题,但是有一个坑,那么就是最后输出不能四舍五入,只能向下取整。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e4 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}
double a[maxn];

bool judge(double mid){
  int ans = 0;
  for(int i = 0; i < n; ++i)  ans += (int)(a[i] / mid);
  return ans >= m;
}

double solve(){
  double l = 0, r = INF;
  for(int i = 0; i < 100; ++i){
    double mid = (l+r) / 2.0;
    if(judge(mid))  l = mid;
    else r = mid;
  }
  return floor(l*100) / 100;
}

int main(){
  while(scanf("%d %d", &n, &m) == 2){
    for(int i = 0; i < n; ++i)  scanf("%lf", a+i);
    printf("%.2f\n", solve());
  }
  return 0;
}

 

POJ 1064 Cable master (二分)