poj3287 The Trip, 2007

Description

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.

 

Input

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.

 

Output

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

 

Sample Output

3
1 2
1 2
3 2

 1 /*
 2 预处理一下,排好序
 3 统计哪个尺寸的背包最多
 4 容易想到 那个尺寸的背包只能分开到不同的piece中去
 5 然后开始从头分配包包
 6 第一个分到piece 1,第二个分到piece 2……第piece+1个分到piece 1
 7 如此循环。这样保证了每个piece中的包满足相互嵌套
 8 又保证了所有piece的背包数量的最大值达到最小(因为是尽可能平均分的)
 9 
10 输出格式貌似比较严格:
11 1每一行最后面不要出现多余空格
12 2最后一个case后面不要出现空行
13 */
14 #include <iostream>
15 #include<stdio.h>
16 #include<string.h>
17 #include<algorithm>
18 using namespace std;
19 #define N 1000100
20 int c[N],a[N];
21 int main() {
22     int n,i,u,j;
23     while(scanf("%d",&n)!=EOF&&n){
24         u=0;
25         memset(c,0,sizeof(c));
26         for(i=0;i<n;i++){
27             scanf("%d",&a[i]);
28             c[a[i]]++;
29             if(c[a[i]]>u) u=c[a[i]];
30         }
31         sort(a,a+n);
32         printf("%d\n",u);
33         for(i=0;i<u;i++){
34             printf("%d",a[i]);
35             for(j=i+u;j<n;j+=u){
36                 printf(" %d",a[j]);
37             }
38             printf("\n");
39         }
40     }
41     return 0;
42 }

转载于:https://www.cnblogs.com/z-712/p/7301385.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值