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. 【转】【CTO俱乐部走进支付宝】探索支付宝背后的那些技术 部分

    怎么避免研发环节当中引入漏洞?“我们有一套比较成熟的安全研发开发框架.首先我们用统一的框架,框架本身是经过我们安全工程师去做过非常好的认证的,同时在研发过程当中我们对这些问题也有些规范.一旦发现风险我 ...

  2. 第二百一十四节,jQuery EasyUI,Calendar(日历)组件

    jQuery EasyUI,Calendar(日历)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Canlendar(日历)组件的使用方法 ...

  3. 消息队列ipc的一些设置

    Linux IPC 参数设定- 命令方式: echo 80 > /proc/sys/vm/overcommit_ratio, etc MSGMNB 每个消息队列的最大字节限制. MSGMNI 整 ...

  4. java网络编程5-SSL

    服务器端 System.out.println("等待客户端连接..."); File keyFile=new File("C:/Users/mu/Desktop/mu. ...

  5. WPF之路——用户控件对比自定义控件UserControl VS CustomControl)

    将多个现有的控件组合成一个可重用的“组”. 由一个XAML文件和一个后台代码文件. 不能使用样式和模板. 继承自UserControl类. 自定义控件(扩展) 在现有的控件上进行扩展,增加一些新的属性 ...

  6. 源码分析——Action代理类的工作

     Action代理类的新建 通过<Struts2 源码分析——调结者(Dispatcher)之执行action>章节我们知道执行action请求,最后会落到Dispatcher类的serv ...

  7. 开源的Eclipse的文件转码插件,可以在不影响中文的情况下改变项目文件编

    http://www.blogjava.net/lifesting/archive/2008/04/11/192250.html, 感谢此作者! 问题描述: 我们项目开发都统一采用utf-8格式编码, ...

  8. Appcompat实现Action Bar的兼容性处理

    Appcompat实现Action Bar时,如果使用到split action bar或者Navigating Up with the App Icon需要考虑兼容性.下面介绍下split acti ...

  9. 俄罗斯方块——shell

    #!/bin/bash # Tetris Game # xhchen<[email]xhchen@winbond.com.tw[/email]> #APP declaration APP_ ...

  10. git 四个基本对象、分支、三个存储区、reset-revert-变基、cherry-pick

    1:git四个基本对象     2:工作区.缓存去.历史区   3:Git 分支介绍 https://blog.csdn.net/wh_19910525/article/details/7470964 ...