uva 11100- The Trip

11100 - The Trip, 2007

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=2041

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 integer1 ≤ n ≤ 10000 giving the number of bags followed byn integers 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
題目大意:

有 n 個包,大包可以裝一個小包,問這些包能組成的最少包數,且要把不相同的放在一起

題解: 找出 n 個數中出現最多次數的數看,即形成的最少包數,從小到大排序,序後,間隔k輸出即可,因爲k是出現最多的數,所以每隔k個輸出保證不會相同,同時每組包的數目又最小。

            例如:

                     1122334444556666 可以分成 1346,1346,2456 2456四個,即 k =4;

代碼:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int a[10010],b[10010];
int main()
{
    int n;
    while(cin>>n&&n)
    {
        memset(b,0,sizeof(b));
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            b[a[i]]++;
        }
        int maxn=b[0];
        for(int i=0;i<10010;i++)
        {
            maxn=max(maxn,b[i]);
        }
        cout<<maxn<<endl;
        sort(a,a+n);
        for(int i=0;i<maxn;i++)
        {
            cout<<a[i];
            for(int j=i+maxn;j<n;j+=maxn)
                cout<<" "<<a[j];
            cout<<endl;
        }
        cout<<endl;
    }
}


       

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