problem f:the trip, 2007Time limit: 1 Sec Memory limit: 128 MB Special Judge
Submitted: 10 Settlement: 5
Topic Description
A number of students are members of the 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 trips but aren ' t quite sure where or when.
A issue with the "trip is" their very generous sponsors always give them various-knapsacks and other carrying bags tha T 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 s O As to minimize the total number of pieces they must carry.
The bags are all exactly the same shape and differ to in their linear-dimension-A which integer not positive G 1000000. A bag with smaller dimension would fit into one with larger dimension. You are are to compute which bags to pack within which others-as to minimize the overall number of pieces of luggage (i.e. The number of outermost bags). While maintaining the minimal number of pieces for you are also to minimize the total number of bags into any one one piece that Mus t be carried.
input
Standard input contains several test cases. Each test case consists of 1≤n≤10000 giving the number of bags followed through n integers on one or more lines, Each giving the dimension of a piece. A line containing 0 follows of the last Test case. Output
For each test case your output should consist of K, the minimum number of pieces, followed by k lines, each giving the dim Ensions of the bags comprising one piece, separated by spaces. Each dimension in the input should appear exactly once in the output, and the bags at each piece must fit nested one Withi N another. If there is more than one solution, the any would do. Output an empty line between cases. Sample Input
6
1 1 2 2 2 3
0
Sample Output
3
1 2
1 2 3 2
Method: Greedy
Code:
#include<stdio.h>#include<stdlib.h>intN, num[10005], Max, he;//Global array not initialized, all 0intCmpinc (const void*a,const void*B) { return(*(int*) a-* (int*) (b); }voidSort_minnum () { for(inti = 0; I < n; i + +) {if(Num[i] = = num[i + 1])
{He + +; }Else{if(Max < He)
{Max = he;
he = 1; } printf ("%d\n", Max); }voidSort_allsteps () { for(inti = 0; i < Max; i + +) { for(intj = i; J < N; j = Max) {printf ("%d", Num[j]);if(j + Max < n) printf (" ");Elseprintf"\ n"); }
}
}intMain () {//int bo = 0; while(~SCANF ("%d", &n) && N) {//if (Bo = = 0)//bo = 1;/else//printf ("\ n"); Max = 1; he = 1; for(inti = 0; I < n; i + +) scanf ("%d", &num[i]); Qsort (Num,n,sizeof(Num[0]), cmpinc); C Language of the fast row function, stored in the Stdlib.h library, the first parameter is the array name, the second parameter is an array of length, the third parameter is a single element size, the fourth parameter is the comparison function sort_minnum ();/Find the minimum number of packages needed Sort_al Lsteps ()//Find a way to sort all the packages printf ("\ n"); } return0; }