NC24049. [USACO 2017 Jan P]Building a Tall Barn
描述
Farmer John is building a brand new, N-story barn, with the help of his K cows ( and ). 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 N total floors in the barn, and each floor must have at least one cow assigned to it. The ith floor requires units of total work, and each cow completes one unit of work per hour, so if c cows work on floor i, it will be completed in units of time. For safety reasons, floor i must be completed before construction can begin on floor i+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.
输入描述
The first line of input contains N and K.
The next N lines contain , each a positive integer of size at most .
输出描述
Please output the minimum time required to build the barn, rounded to the nearest integer.
示例1
输入:
2 5 10 4
输出:
5
C++(clang++11) 解法, 执行用时: 97ms, 内存消耗: 1144K, 提交时间: 2020-12-12 12:09:33
#include<bits/stdc++.h> #define Ll long long using namespace std; const Ll N=1e5+5; double a[N],l,r,mid,ans,T; Ll x,k,sum,n; bool check(double t){ Ll sum=0; for(Ll i=1;i<=n;i++){ double c=(sqrt(1+4*a[i]/t)-1)/2.; Ll v=ceil(c); sum+=v; if(sum>k)return 0; }return 1; } int main() { scanf("%lld%lld",&n,&k); for(Ll i=1;i<=n;i++)scanf("%lld",&x),a[i]=x; l=1e-9,r=12; while(r-l>1e-9){ mid=(l+r)/2.; if(check(mid))r=mid;else l=mid; } for(Ll i=1;i<=n;i++){ double c=(sqrt(1+4*a[i]/r)-1)/2.; Ll v=ceil(c); sum+=v; ans+=a[i]/v; } printf("%.0lf",ans-(k-sum)*r); }