[CCPC2019 ONLINE]H Fishing Master

题意

http://acm.hdu.edu.cn/showproblem.php?pid=6709


思考

先考虑所有鱼的烹饪时间小于k的情况。将T从大到小排序后,煮一条鱼相当于将其时间补齐至k。

由于鱼的烹饪时间会大于等于k,那么,最优解一定不会出现鱼不够煮的情况。在煮鱼期间,可以钓到$frac{t_i}{k}$条鱼,最后要补齐的鱼的数量有n-cnt-1条。-1是因为最开始没有鱼。


代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long int ll;
 4 const int maxn=1E5+5;
 5 int T;
 6 ll n,k;
 7 ll t[maxn];
 8 bool cmp(ll x,ll y)
 9 {
10     return x>y;
11 }
12 void solve()
13 {
14     cin>>n>>k;
15     ll cnt=0,ans=0;
16     for(int i=1;i<=n;++i)
17     {
18         cin>>t[i];
19         ans+=t[i];
20         cnt+=t[i]/k;
21         t[i]%=k;
22     }
23     cnt=min(cnt,n);
24     sort(t+1,t+n+1,cmp);
25     for(int i=1;i<=n-cnt-1;++i)
26         ans+=k-t[i];
27     cout<<ans+k<<endl;
28 }
29 int main()
30 {
31     ios::sync_with_stdio(false);
32     cin>>T;
33     while(T--)
34         solve();
35     return 0;
36 }
View Code
原文地址:https://www.cnblogs.com/GreenDuck/p/11410503.html