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. Installing scipy on redhat with error “no lapack/blas resources found”

    这是更新scipy出现的结果,需要新版本的scipy,而机器上只装了0.7的版本,更新的时候报错,找到了一个解决方法: wget http://mirror.centos.org/centos/6/o ...

  2. 关于Jquery Ajax的用法

    今天简单描述一下Jquery Ajax的用法,和我在使用过程中的一些看法,仅供自己娱乐和大家参考值之用! Jquery Ajax的重要性不言而喻,只从Jquery面世之后,终于解救了像我这种既做前台又 ...

  3. mysql DBA 指南

    Mysql目录 数据库介绍.常见分类 Mysql入门 Mysql安装配置 Mysql多实例安装配置 Mysql常用基本命令 Mysql权限体系 Mysql数据库备份和恢复 Mysql日志 Mysql逻 ...

  4. 读取csv格式的数据

    1.直接上代码,关键是会用 2.代码如下: <?php #添加推荐到英文站 $file = fopen('code.csv','r'); while ($data = fgetcsv($file ...

  5. 《The Swift Programming Language》的笔记-第24页

    The Swift Programming Language读书笔记学习笔记 第24页 本页主要内容有两个:打印输出和怎样在swift凝视代码 1 怎样打印变量和常量的值? 使用println函数,细 ...

  6. Google I/O 2013 – Volley: Easy, Fast Networking for Android

    1.什么是volley          Volley是Ficus Kirpatrick在Gooogle I/O 2013发布的一个处理和缓存网络请求的库,能使网络通信更快,更简单,更健壮.Volle ...

  7. npm and node 的一些问题

    1. node 安装 n 模块 用来管理 node的版本 2.  初始化项目出现这个问题 Error: Attempt to unlock XXX, which hasn't been locked ...

  8. 【Linux】命令学习笔记和总结

    莫名的想学习一下Linux了,因为对这方面的知识储备为0.对于命令行界面始终是零接触零了解,对一个程序员来说这几乎是致命的,所以简单了解一下. 一.教程参考 参考菜鸟教程即可: Linux 教程 | ...

  9. Pat 1052 Linked List Sorting (25)

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  10. KMS Event LOG

    The 12290 event entry gives a significant amount of information that can be used to figure out what ...