Kattis - fence2【二分法】

题意

有一个农夫需要建造一个 N - 1 米长的篱笆,需要 N 根柱子,然后有 K 根 柱子 需要将这 K 根柱子 切成 N 段 然后 要尽量保证这 N 段柱子的长度尽量长,并且这 N 段柱子的长度是相等的 然后求最少需要切几次。注意:柱子的长度可以是浮点数。

思路

用二分法找答案,然后再求和 不过要注意的是 ,如果柱子原来的长度与要分成的柱子的长度是可以整除的 那么切的刀数 就是 N/MAX - 1

AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;
typedef long long LL;
const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6;
const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e4 + 5;
const int MOD = 1e9 + 7;
int arr[maxn];
int n, k;
bool check(double mid)
{
int sum = 0;
for (int i = 0; i < k; i++)
{
int temp = (int)(1.0 * arr[i] / mid);
if (fabs(1.0 * temp * mid - 1.0 * arr[i]) <= eps)
temp--;
sum += temp;
}
if (sum >= n)
return true;
return false;
}
int main()
{
scanf("%d%d", &k, &n);
memset(arr, 0, sizeof(arr));
for (int i = 0; i < k; i++)
scanf("%d", &arr[i]);
double l = 0, r = (double)n;
double mid;
double MAX = MINN;
while (r - l >= eps)
{
mid = (l + r) / 2.0;
if (check(mid))
{
MAX = max(MAX, mid);
l = mid;
}
else
r = mid;
}
LL ans = 0;
for (int i = 0; i < k; i++)
{
int temp = (int)(1.0 * arr[i] / MAX);
if (fabs(1.0 * temp * MAX - 1.0 * arr[i]) <= 1e-1)
temp--;
ans += temp;
}
cout << ans << endl;
}

Kattis - fence2【二分法】的更多相关文章

  1. C语言两种查找方式(分块查找,二分法)

    二分法(必须要保证数据是有序排列的):   分块查找(数据有如下特点:块间有序,块内无序):    

  2. poj3122-Pie(二分法+贪心思想)

    一,题意: 有f+1个人(包括自己),n块披萨pie,给你每块pie的半径,要你公平的把尽可能多的pie分给每一个人 而且每个人得到的pie来自一个pie,不能拼凑,多余的边角丢掉.二,思路: 1,输 ...

  3. 二分法&三分法

    ural History Exam    二分 #include <iostream> #include <cstdlib> using namespace std; //二分 ...

  4. [No000087]Linq排序,SortedList排序,二分法排序性能比较

    using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; ...

  5. [PHP]基本排序(冒泡排序、快速排序、选择排序、插入排序、二分法排序)

    冒泡排序: function bubbleSort($array){ $len=count($array); //该层循环控制 需要冒泡的轮数 for($i=1;$i<$len;$i++){ / ...

  6. iOS常见算法(二分法 冒泡 选择 快排)

    二分法: 平均时间复杂度:O(log2n) int halfFuntion(int a[], int length, int number)  { int start = 0; int end = l ...

  7. java简单的二分法排序

    二分法排序的思路:数据元素要按顺序排列,对于给定值 x,从序列的中间位置开始比较,如果当前位置值等于 x,则查找成功:若 x 小于当前位置值,则在数列的前半段中查找:若 x 大于当前位置值则在数列的后 ...

  8. 使用二分法查找mobile文件中区号归属地

    #!/usr/bin/env python #coding:utf-8 ''' Created on 2015年12月8日 @author: DL @Description: 使用二分法查找mobil ...

  9. Atitit 迭代法  “二分法”和“牛顿迭代法 attilax总结

    Atitit 迭代法  "二分法"和"牛顿迭代法 attilax总结 1.1. ."二分法"和"牛顿迭代法"属于近似迭代法1 1. ...

随机推荐

  1. Windows动态库的使用 part one

    二.动态库程序 这篇写得比较乱,看不懂的可以跳part 2,直接上图片的.   动态库的使用: 4.3.1 隐式链接 动态库的隐式连接,需要动态库的 动态库文件".dll" , 动 ...

  2. 完成blog后台一枚

    技术实现:纯jfinal+AmazeUI

  3. Android学习15--使用(Drawable)资源

    1.图片资源 图片资源是最简单的Drawable资源.仅仅要把*.png.*.jpg*..gif等格式的图片放入/res/drawable-XXX文件夹下,Android SDK就会在编译应用自己主动 ...

  4. 常用 Git 命令文档和命令

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA3IAAAEVCAIAAAAq20B9AAAgAElEQVR4nOydd3wUxfvH93p6gQRCCF ...

  5. js ie 6,7,8 使用不了 firstElementChild

    一. <div> <p>123</p> </div> 在上面这段代码中,如果使用以下js代码 var oDiv=document.getElementB ...

  6. Toxophily-数论以及二分三分

    G - Toxophily Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  7. Android中流式布局和热门标签

    1.流式布局特点.应用场景.2.自定义ViewGroup (1)onMeasure:测量子View的宽和高,设置自己的宽和高. (2)onLayout:设置子View的位置. onMeasure:根据 ...

  8. 160629、 DBCP、C3P0、Proxool 、 BoneCP开源连接池的比较

       简介   使用评价  项目主页  DBCP DBCP是一个依赖Jakarta commons-pool对象池机制的数据库连接池.DBCP可以直接的在应用程序用使用 可以设置最大和最小连接,连接等 ...

  9. Byzantine failures

    https://baike.baidu.com/item/拜占庭将军问题/265656?fr=aladdin 拜占庭将军问题(Byzantine failures),是由莱斯利·兰伯特提出的点对点通信 ...

  10. HTTP 错误 500.21 - Internal Server Error 解决方案(转)

    不久前重新安装了Windows7,在安装了VS2010 开发平台之后,将网站发布到IIS,访问发生如下错误: HTTP 错误 500.21 - Internal Server Error处理程序“Ni ...