NC223520. Convoy
描述
输入描述
The first line of input contains two space-separated integers n and k (1 ≤ n, k ≤ 20 000), the number of people atyour house and the number of available cars. Then follow n lines containing a single integer each; the such integeris the number of seconds (1 ≤≤ 1 000 000) that it takes person i to drive from your house to the stadium, orvice-versa
输出描述
Print the minimum number of seconds it takes to move all n people from your house to the stadium, if all people coordinate and drive optimally.
示例1
输入:
11 2 12000 9000 4500 10000 12000 11000 12000 18000 10000 9000 12000
输出:
13500
示例2
输入:
6 2 1000 2000 3000 4000 5000 6000
输出:
2000
C++ 解法, 执行用时: 13ms, 内存消耗: 512K, 提交时间: 2021-08-20 09:36:00
#include<bits/stdc++.h> using namespace std; typedef long long ll; int a[20005]; int n,k; int fun(ll x) { ll sum=0; for(int i=1;i<=k;i++) { if(x>=a[i]) { sum=sum+5+(x-a[i])/(2*a[i])*4; if(sum>=n) return 1; } } return 0; } int main() { cin>>n>>k; k=min(n,k); for(int i=1;i<=n;i++) { cin>>a[i]; } sort(a+1,a+1+n); ll l=0,r=1e11,mid; while(l<r) { mid=(l+r)>>1; if(fun(mid)) r=mid; else l=mid+1; } cout<<l; return 0; }