A number of students are members of
a club that travels annually to exoticlocations. Their destinations in thepast have included Indianapolis, Phoenix,Nashville, Philadelphia, San Jose, Atlanta,Eindhoven, Orlando, Vancouver,Honolulu, Beverly Hills, Prague, Shanghai,and San Antonio. This spring theyare hoping to make a similar trip butaren’t quite sure where or when.An issue with the trip is that their verygenerous sponsors always give them variousknapsacks and other carrying bagsthat 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 onebag 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 positiveinteger not exceeding 1000000. A bag with smaller dimension will fit in one with larger dimension. Youare to compute which bags to pack within which others so as to minimize the overall number of piecesof luggage (i.e. the number of outermost bags). While maintaining the minimal number of pieces youare also to minimize the total number of bags in any one piece that must be carried.InputStandard input contains several test cases. Each test case consists of an integer 1 ≤ n ≤ 10000 givingthe number of bags followed by n integers on one or more lines, each giving the dimension of a piece.A line containing 0 follows the last test case.OutputFor 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 inthe input should appear exactly once in the output, and the bags in each piece must fit nested onewithin another. If there is more than one solution, any will do. Output an empty line between cases.Sample Input61 1 2 2 2 30Sample Output31 21 23 2解题思路:题目的大概意思是将给出的正整数尽量少的分割成严格递增的规格。可以用一个数组记录出每个数字出现的次数,求出出现次数最多的数字的个数,这里有一个技巧,可以利用周期来解答。求出出现次数最多的数出来之后,可以利用周期,因为要求每一组中一个数只能出现一次,利用出现次数最多的那个数的个数作为周期,那么一个组中就不可能出现两个相同的数了。利用这一特点,做这个题目就简单好多。
程序代码:
#include"stdio.h"#include"string.h"#include"algorithm" using namespace std; const int N=10010; const int M=1000010; int a[N]; int num[M]; int main() { int n,i,j,counta; while(scanf("%d",&n)&&n) { counta=0; memset(num,0,sizeof(num)); for(i=0;i