Sicily 2015. A New Year Gift 解题报告

题目:

Description

Windbreaker was planning to send his friends some necklaces as New Year gifts. To show sincerity ,he decided to make the necklaces all by himself. He bought some kinds of pearls and each kind of pearls has a different color from others. He wanted to make each necklace consisted of M pearls from different kinds, that’s what he called M-perfect necklace. Windbreaker wanted to know the maximum number of necklaces he could send out to his friends. The number of pearls of each kind and M are given, and now you are asked to tell Windbreaker how many M-perfect necklaces he could make at most

Input

You will be given a number of cases in the input. Each case starts with a positive integer n(n<=1000),which is the number of different kinds;

The second line contains n positive numbers represent for the number of pearls of each kind which will not exceed 2000.

The third line contains a positive number M(1<=M<=100).

The end of input is indicated by a line containing n=0.

Output

For each test case, output a line containing the Maximum number of M-perfect necklaces.

Sample Input

5
3 3 3 3 3
5
6
1 2 3 4 5 6
5
0

Sample Output

3
3

思路:

一开始用方法是将珍珠按照数量从小到大排序,然后从大的开始选起,选完后将大的m种数量都减少1,再进行排序,以此类推。

直到pearls[n-m]的数目为0的时候停止。结果发现由于排序次数太多肯定要超时。然后尝试一次过将pearls[n-m]减少到0,即一次选出pearls[n-m]条,发现时间满足了但是其实结果已经改变了,因为这样不能满足贪心算法每次都从最大的m种珍珠中选取。最后参考别人的解题思路只好用二分法。

二分法思路也很清晰,用top表示最多能产生的条数,bottom表示最少能产生的条数,每次试验能否产生mid条项链。检验时,重新计算每一次的sum,如果mid大于pearls[i],则只能用上pearls[i]个,否则加上mid个珍珠。对于能否产生k条项链,只需要sum>=k*m即可,否则配不出k条。

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 int main(){
 6     int n,m;
 7     while(cin>>n&&n!=0){
 8         int pearls[n];
 9         int bottom=0,top,mid,sum=0;//用二分法逼近最后能形成的项链数
10         for(int i=0;i<n;i++){
11             cin>>pearls[i];
12             sum+=pearls[i];
13         }
14         cin>>m;
15         top=sum/m;//理论上能产生的最多项链数
16         while(bottom<top){
17             mid=(bottom+top)/2;//判断能不能产生mid条项链
18             sum=0;
19             for(int i=0;i<n;i++){//由贪心算法,对于每种珍珠数量和mid之间的较少者min,
20                 if(pearls[i]>mid)//总有办法使得那min个珍珠被用上在形成的项链中
21                     sum+=mid;
22                 else
23                     sum+=pearls[i];
24             }
25             if(mid*m>sum)
26                 top=mid-1;
27             else
28                 bottom=mid+1;
29         }
30         sum=0;
31         for(int i=0;i<n;i++){
32             if(pearls[i]>bottom)
33                 sum+=bottom;
34             else
35                 sum+=pearls[i];
36         }
37         if(bottom*m>sum)//最后一次判断用来检测二分逼近时最后的bottom=mid+1可不可取
38             cout<<bottom-1<<endl;
39         else
40             cout<<bottom<<endl;
41     }
42     return 0;
43 }

附:参考代码

#include <cstdio>
using namespace std;

int main(){
    int n,M,i;
    int bottom,top,mid;
    int sum,data[1000];
    while(scanf("%d",&n)&&n!=0){
        sum=0;//所有种类珍珠的总数
        for(i=0;i<n;i++){
            scanf("%d",&data[i]);
            sum+=data[i];
        }
        scanf("%d",&M);
        bottom=0;
        top=sum/M;//最多可以组成多少条项链
        while(bottom<top){
            mid=(bottom+top)/2;
            sum=0;
            for(i=0;i<n;i++)
                sum+=(data[i]>=mid?mid:data[i]);
            sum>=mid*M?bottom=mid+1:top=mid-1;
        }
        sum=0;
        for(i=0;i<n;i++)
            sum+=(data[i]>=bottom?bottom:data[i]);
        printf("%d
",sum>=bottom*M?bottom:bottom-1);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jolin123/p/3325455.html