hdu 6709 Fishing Master greedy

Feeling that Italy was not clear, the hands can be kept waiting for more than grilled fish.

Assuming that the time required for each fish cooked very long, then we begin to catch a fish, then this surface grilled fish, catch a fish, baked waiting for this, this cycle. Then the total time is k + Σt [i].

But in fact there may be some grilled fish particularly fast, this baked fish face, my face did not catch new fish, causing the stove time wasted.

We consider a fish for i, during its barbecue, we can catch t [i] / k of the fish.

If Σt [i] / k> = n - 1, then we can arrange the order of a rational method of fishing, the furnace so that time is not wasted. The total time is k + Σt [i].

If Σt [i] / k <n - 1, then we will inevitably have to waste some time the furnace. I fish for a barbecue process, we can waste k - t [i]% k of stove time to additional catch a fish.

We look Σt [i] / k and n - 1 gap between how much fish, choose a smaller k - t [i]% k to make up to.

Remember to open long long 

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 typedef long long ll;
 5 int T,n,k,cnt;
 6 ll tim;
 7 int t[100100],lst[100100];
 8 int main()
 9 {
10     for (scanf("%d",&T);T != 0;T--)
11     {
12         scanf("%d%d",&n,&k);
13         tim = k;
14         cnt = 0;
15         for (int i = 1;i <= n;i++)
16             scanf("%d",&t[i]);
17         for (int i = 1;i <= n;i++)
18         {
19             tim += t[i];
20             cnt += t[i] / k;
21             lst[i] = k - t[i] % k;
22         }
23         if (cnt < n - 1)
24         {
25             sort(lst + 1,lst + n + 1);
26             for (int i = 1;i <= n - 1 - cnt;i++)
27                 tim += lst[i];
28         }
29         printf("%lld\n",tim);
30     }
31     return 0;
32 }

 

Guess you like

Origin www.cnblogs.com/iat14/p/11403551.html
Recommended