题目描述

Farmer John is building a brand new, NNN -story barn, with the help of his KKK cows ( 1≤N≤K≤10121 \leq N \leq K \leq 10^{12}1≤N≤K≤1012 and N≤105N \leq 10^5N≤105 ). To build it as quickly as possible, he needs your help to figure out how to allocate work among the cows.

Each cow must be assigned to work on exactly one specific floor out of the NNN total floors in the barn, and each floor must have at least one cow assigned to it. The iii th floor requires aia_iai​ units of total work, and each cow completes one unit of work per hour, so if ccc cows work on floor iii , it will be completed in ai/ca_i / cai​/c units of time. For safety reasons, floor iii must be completed before construction can begin on floor i+1i+1i+1 .

Please compute the minimum total time in which the barn can be completed, if the cows are allocated to work on floors in an optimal fashion. Output this number rounded to the nearest integer; it is guaranteed that the solution will be more than 0.1 from the boundary between two integers.

FJ正在他的K头奶牛的帮助下建造一个全新的N层谷仓(1<=N<=K<=10^12,N<=10^5)。为了能够尽快的建造它,他需要你帮助他来找出如何在奶牛间分配工作。

每一头牛必须分配到N层中的某一个特定楼层中,并且每一层楼必须至少有一头牛在其中工作。第i层楼需要a[i]个单位的工作,并且每一头牛完成每一单位的工作需要一个单位时间。所以如果有C头牛在第i层工作,那么第i层需要a[i]/c个单位时间。为了安全起见,在施工开始于在第i+1层楼上前,必须先完成第i层。

如果奶牛被分配以最佳方式在楼层上工作,请计算完成谷仓的最小总时间。输出这个时间四舍五入到整数的结果;题目保证解答离两个整数之间的边界大于0.1。

输入输出格式

输入格式:

The first line of input contains NNN and KKK .

The next NNN lines contain a1…aNa_1 \ldots a_Na1​…aN​ , each a positive integer of size at most 101210^{12}1012 .

第一行包括两个数N和K。

接下来N行包括了a[1]...a[n],每行一个不大于10^12的正整数。

输出格式:

Please output the minimum time required to build the barn, rounded to the

nearest integer.

请输出完成谷仓的最小总时间(四舍五入至最接近的整数)。

输入输出样例

输入样例#1:

2 5
10
4
输出样例#1:

5

提交地址: Luogu3606

答案抄的真爽, 就是抄了一晚上,WA了n次,才找出来错误, 崩溃ing...

这道题其实一眼就知道二分答案, 想都没想直接敲,16分,what...f?

回过头再仔细读题解, 发现这题并不简单;

我们知道a[1],a[2]...a[n],也知道k,我们要求a[1]/b[1]+a[2]/b[2]+...+a[n]/b[n]的最小值,其中Σbi=k;

这仿佛不是一个寻常的二分可以搞定的;

仔细琢磨题解你会发现,我们要让其中每一层楼,不加入一个牛,比加入一个牛更优,什么意思?
就是我们考虑, 如果这个楼层加一个牛比不加一个牛优,那我们肯定会把别的楼层的牛拿过来给他;
那我们该怎么表示这个抽象的东西呢? 我们设:ti = ai / (ci + 1) - ai / ci , ci是第i层楼的牛数量;
这样我们设出来的ti,就表示这个楼层多一只牛的改变量,我们要求的最终答案,肯定是每层楼的ti最接近,否则我们肯定可以找一只牛放里面; 化简一下得到 ti = ai / (ci * (ci + 1)) ,移项得 ci^2 - ci - ai/ti = 0; 发现我们只要枚举ti, 就可以通过简单的小学数学,把ci算出来; 然后开开心心的二分; 最后如果有空闲的牛就直接乘以r,然后减掉就行了,为什么要乘r?
因为我们二分出来了一个ti,这个ti保证是所有楼层的ti(因为他们最接近的时候才是答案),所以直接乘ti,当成他们的贡献! 有几个坑点,首先记得开long long,还得记得二分一个小数一定要设eps... Code:
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define int long long
#define ll long long
double eps = 1e-;
ll n, k;
double a[]; double l = 1e-, r = , mid;
double ans;
ll now; inline bool check(double x)
{
now = ;
double res = ;
for (register ll i = ; i <= n ; i ++)
{
double h = (sqrt( + * a[i] / x) - ) / ;
ll f = ceil(h);
now += f;
if (now > k) return ;
}
return ;
} signed main()
{
scanf("%lld%lld", &n, &k);
for (register ll i = ; i <= n ; i ++) cin >> a[i]; while (r - l > eps)
{
mid = (l + r) / ;
bool fl = check(mid);
if (fl) r = mid;
else l = mid;
} ans = 0.0, now = ;
for (register ll i = ; i <= n ; i ++)
{
double h = (sqrt( + * (a[i] / r)) - ) / ;
ll f = ceil(h);
ans += a[i]/f;
now += f;
} printf("%.0lf", ans - (k - now) * r);
return ;
}

[USACO17JAN]Building a Tall Barn建谷仓的更多相关文章

  1. [luoguP3606] [USACO17JAN]Building a Tall Barn建谷仓(贪心 + 线段树)

    传送门 把线段都读进来然后排序,先按右端点为第一关键字从小到大排序,后按左端点为第二关键字从小到大排序. 注意不能先按左端点后按右端点排序,否则会出现大包小的情况,如下: —————— ———  — ...

  2. [USACO07FEB]新牛棚Building A New Barn

    洛谷题目链接:[USACO07FEB]新牛棚Building A New Barn 题目描述 After scrimping and saving for years, Farmer John has ...

  3. Bzoj 1696: [Usaco2007 Feb]Building A New Barn新牛舍 中位数,数学

    1696: [Usaco2007 Feb]Building A New Barn新牛舍 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 394  Solve ...

  4. 洛谷 2957 [USACO09OCT]谷仓里的回声Barn Echoes

    题目描述 The cows enjoy mooing at the barn because their moos echo back, although sometimes not complete ...

  5. 洛谷P2874 [USACO07FEB]新牛棚Building A New Barn [贪心]

    题目传送门 题目描述 After scrimping and saving for years, Farmer John has decided to build a new barn. He wan ...

  6. 洛谷——P2957 [USACO09OCT]谷仓里的回声Barn Echoes

    https://www.luogu.org/problem/show?pid=2957 题目描述 The cows enjoy mooing at the barn because their moo ...

  7. [USACO09OCT]谷仓里的回声Barn Echoes(hush、STL)

    https://www.luogu.org/problem/P2957 题目描述 The cows enjoy mooing at the barn because their moos echo b ...

  8. [luoguP2957] [USACO09OCT]谷仓里的回声Barn Echoes(Hash)

    传送门 团队里的hash水题,数据小的不用hash都能过.. 也就是前缀hash,后缀hash,再比较一下就行. ——代码 #include <cstdio> #include <c ...

  9. TZOJ 1689 Building A New Barn(求平面上有几个其它点求到n个点的曼哈顿距离最小)

    描述 After scrimping and saving for years, Farmer John has decided to build a new barn. He wants the b ...

随机推荐

  1. 7、创建图及图的遍历(java实现)

    1.顺序表用于图的深度优先遍历 public class SeqList { public final int MaxSize = 10; public Object list[]; public i ...

  2. mapper 传多个参数

    Mybatis的Mapper接口的参数,一般是一个对象,但如果不是对象,并且有多个参数的时候呢?我们第一个的想法是把参数封装成一个java.util.Map类型,然后在方法的注释上面写上map的key ...

  3. Linux 笔记 - 第十四章 LAMP 之(二) 环境配置

    博客地址:http://www.moonxy.com 一.前言 LAMP 环境搭建好之后,其实仅仅是安装上了软件,我们还需要掌握 httpd 和 PHP 的配置. 二.httpd 配置 2.1 创建虚 ...

  4. leetcode - 最小移动次数使数组元素相等

    思路 (假设数组每次都已被排好序) 每次使得小于等于最大值的n-1的数字加1,直接暴力求解会超时 改进一: 为了让最小元素等于最大元素,至少需要数组中最大值-最小值次, 所以以此为基础再次暴力求解(参 ...

  5. 树莓派4B安装docker-compose(64位Linux)

    准备工作 树莓派4B已装好64位Linux,并且装好了19.03.1版本的Docker,具体的安装步骤请参考<树莓派4B安装64位Linux(不用显示器键盘鼠标)> 安装docker-co ...

  6. 微信图片解决方法-windows版的dat文件

    public string decodeImg(string filepath) { Dictionary<string, byte[]> headers = new Dictionary ...

  7. Java程序连接数据库

    /** * 了解: 利用 Driver 接口的 connect 方法获取连接 */ // 第一种实现 /** * 了解: 利用 Driver 接口的 connect 方法获取连接 */ @Test p ...

  8. 使用T2表中的值替换T1表的值

    描述:现在有两张表,T1由Key和Value两个字段,T2也有Key和Value两个字段 当T1中的Key在T2表中存在时,更新使用T2表中对用的Value 值替换T1中的VAlue update A ...

  9. SpringBoot 2.0 + InfluxDB+ Sentinel 实时监控数据存储

    前言 阿里巴巴提供的控制台只是用于演示 Sentinel 的基本能力和工作流程,并没有依赖生产环境中所必需的组件,比如持久化的后端数据库.可靠的配置中心等.目前 Sentinel 采用内存态的方式存储 ...

  10. 月光宝盒之时间魔法--java时间的前生今世

    月光宝盒花絮 “曾经有一份真诚的爱情摆在我的面前,但是我没有珍惜,等到了失去的时候才后悔莫及,尘世间最痛苦的事莫过于此.如果可以给我一个机会再来一次的话,我会跟那个女孩子说我爱她,如果非要把这份爱加上 ...