[貪心]UVA11100 - The Trip, 2007

Problem A: The Trip, 2007

A number of students are members of a club that travels annually to exotic locations. Their destinations in the past have included Indianapolis, Phoenix, Nashville, Philadelphia, San Jose, Atlanta, Eindhoven, Orlando, Vancouver, Honolulu, Beverly Hills, Prague, Shanghai, and San Antonio. This spring they are hoping to make a similar trip but aren't quite sure where or when.

An issue with the trip is that their very generous sponsors always give them various knapsacks and other carrying bags that they must pack for their trip home. As the airline allows only so many pieces of luggage, they decide to pool their gifts and to pack one bag within another so as to minimize the total number of pieces they must carry.

The bags are all exactly the same shape and differ only in their linear dimension which is a positive integer not exceeding 1000000. A bag with smaller dimension will fit in one with larger dimension. You are to compute which bags to pack within which others so as to minimize the overall number of pieces of luggage (i.e. the number of outermost bags). While maintaining the minimal number of pieces you are also to minimize the total number of bags in any one piece that must be carried.

Standard input contains several test cases. Each test case consists of an integer 1 ≤ n ≤ 10000 giving the number of bags followed by nintegers on one or more lines, each giving the dimension of a piece. A line containing 0 follows the last test case. For each test case your output should consist of k, the minimum number of pieces, followed by k lines, each giving the dimensions of the bags comprising one piece, separated by spaces. Each dimension in the input should appear exactly once in the output, and the bags in each piece must fit nested one within another. If there is more than one solution, any will do. Output an empty line between cases.

Sample Input

6
1 1 2 2 2 3
0

Output for Sample Input

3
1 2
1 2
3 2

Troy Vasiga, Graeme Kemkes, Ian Munro, Gordon V. Cormack

題意:有一個團隊要外出旅行,需要帶很多包,大包裏面能放小包,問這些包最後剩下多少個包。

思路:貪心,先排序,由於大包裏面能放小包,所以最後剩下的包的個數是相同大小的包的個數的最大數

#include<iostream>
#include<algorithm>

using namespace std;

int node[10005];


int main()
    {
        int num,tag=0;
        while(cin>>num&&num)
            {
                int i,j,k;
                for(i=0;i<num;i++)
                    {
                        cin>>node[i];
                    }
                sort(node,node+num);
                int cnt=1,len=-1;
                for(i=1;i<num;i++)
                    {
                        if(node[i]==node[i-1])
                            {
                                cnt++;
                                if(i==num-1&&cnt>len) len=cnt;
                            }
                        else
                            {
                                if(cnt>len) len=cnt;
                                cnt=1;
                            }
                    }
                if(tag==0) tag=1;
                else cout<<endl;
                cout<<len<<endl;
                for(i=0;i<len;i++)
                    {
                        int flag=0;
                        for(j=i;j<num;j=j+len)
                            {
                                if(flag==0)
                                    {
                                        cout<<node[j];
                                        flag=1;
                                    }
                                else
                                    {
                                        cout<<" "<<node[j];
                                    }
                            }
                        cout<<endl;
                    }
            }
        return 0;
    }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章