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 n 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

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

题目大意:给定N个整数,把他们划分成尽量少的严格递增序列。

题解:先对N个数进行升序排序,然后统计出数量最多的那个元素的数量K,则K就是序列个数,因为相同的元素是不能放在同一个序列中的。之后就是构造出每个序列。方法很巧妙,具体请参看程序。

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define MAXN 1000005
 5 long a[10005];
 6 long ans[MAXN];
 7 long n;
 8 int compare(const void*a,const void*b)
 9 {
10     return *(int*)a-*(int*)b;
11 }
12 int main(void)
13 {
14     long i,j,k,m;
15     while(scanf("%ld",&n)!=EOF)
16     {
17         if(n==0) break;
18         for(i=0; i<n; i++)
19             scanf("%ld",&a[i]);
20         memset(ans,0,sizeof(ans));
21         k=-1;
22         for(i=0;i<n;i++)
23         {
24             ans[a[i]]++;
25             if(ans[a[i]]>k) k=ans[a[i]];
26         }
27         m=n/k+2;
28         long f[k+1][m];
29         qsort(a,n,sizeof(a[0]),compare);
30         memset(f,0,sizeof(f));
31         for(i=0;i<n;i++)
32         {   f[i%k][0]++;
33             f[i%k][f[i%k][0]]=a[i];
34         }
35         printf("%ld\n",k);
36         for(i=0;i<k;i++)
37         {
38             for(j=1;j<f[i][0];j++)
39             printf("%ld ",f[i][j]);
40             printf("%ld\n",f[i][j]);
41         }
42     }
43     return 0;
44 }
原文地址:https://www.cnblogs.com/zjbztianya/p/2980809.html