Kattis - fence2【二分法】
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【二分法】的更多相关文章
- C语言两种查找方式(分块查找,二分法)
二分法(必须要保证数据是有序排列的): 分块查找(数据有如下特点:块间有序,块内无序):
- poj3122-Pie(二分法+贪心思想)
一,题意: 有f+1个人(包括自己),n块披萨pie,给你每块pie的半径,要你公平的把尽可能多的pie分给每一个人 而且每个人得到的pie来自一个pie,不能拼凑,多余的边角丢掉.二,思路: 1,输 ...
- 二分法&三分法
ural History Exam 二分 #include <iostream> #include <cstdlib> using namespace std; //二分 ...
- [No000087]Linq排序,SortedList排序,二分法排序性能比较
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; ...
- [PHP]基本排序(冒泡排序、快速排序、选择排序、插入排序、二分法排序)
冒泡排序: function bubbleSort($array){ $len=count($array); //该层循环控制 需要冒泡的轮数 for($i=1;$i<$len;$i++){ / ...
- iOS常见算法(二分法 冒泡 选择 快排)
二分法: 平均时间复杂度:O(log2n) int halfFuntion(int a[], int length, int number) { int start = 0; int end = l ...
- java简单的二分法排序
二分法排序的思路:数据元素要按顺序排列,对于给定值 x,从序列的中间位置开始比较,如果当前位置值等于 x,则查找成功:若 x 小于当前位置值,则在数列的前半段中查找:若 x 大于当前位置值则在数列的后 ...
- 使用二分法查找mobile文件中区号归属地
#!/usr/bin/env python #coding:utf-8 ''' Created on 2015年12月8日 @author: DL @Description: 使用二分法查找mobil ...
- Atitit 迭代法 “二分法”和“牛顿迭代法 attilax总结
Atitit 迭代法 "二分法"和"牛顿迭代法 attilax总结 1.1. ."二分法"和"牛顿迭代法"属于近似迭代法1 1. ...
随机推荐
- is_array
is_array (PHP 4, PHP 5) is_array — 检测变量是否是数组
- Android中TextView和EditView经常使用属性设置
Android开发中最经常使用的几乎相同就是TextView和EditView了,在使用它时.我们也会设置它的一些属性,为了让我们设计的更好看,设置的更合理.这里记下它的经常使用属性,方便后期查阅. ...
- 基于字典SR各种方法【稀疏编码多种方法】
基于字典的图像超分辨率实现 - CSDN博客 http://blog.csdn.net/u011630458/article/details/65635155 简介 这段时间在看基于字典的单帧图像超分 ...
- hdu 4294(bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4294 思路:题目的意思是说,给你n,k,则求出n的正整数倍数,使得这个数字在k进制下表示的时候需要的不 ...
- Windows查看网络端口被占用情况netstat命令
在windows命令行窗口下执行: C:\>netstat -aon|findstr "80" TCP 127.0.0.1:80 0.0.0.0:0 ...
- Redisson教程
Redisson入门 Author:Ricky Date:2017-04-24 Redisson概述 Redisson是架设在Redis基础上的一个Java驻内存数据网格(In-Memory Dat ...
- C Plus Plus 基础
C Plus Plus 基础 一.变量和基本类型 1.const 限定符号 const --> constant(中文意思:不停的,不断的,一直不变的) ①代替Magic Number(即『无意 ...
- C++ 调用webservice 出现 函数返回值为 3 (SOAP_TAG_MISMATCH) 的解决方案
最近在用C++ gsoap做webservice服务时,函数返回值为SOAP_TAG_MISMATCH (==3)错误码,原因是我传入wsdl地址时连同后面的?wsdl都传入了,如下: http:// ...
- poj 1182 食物链 (带关系的并查集)
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44835 Accepted: 13069 Description 动 ...
- 模块 - time/datetime
time 模块 time模块方法: >>> import time >>> time.time() #时间戳 秒级别 1519212085.6211221 #从19 ...