UVa 11100 - The Trip, 2007

【链接】

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

【题目大意】
给出n个数字,代表n个包的大小。小的包可以装到比它大的包里面,可以一直嵌套装下去。让这些包与包装起来,使得变成最少个包。

【分析与总结】
说一下我的第一思路
开始容易就想到了排序,然后会有相同的数字,比如1,2,2,3,3,3,4,4,4,4, 为了尽可能的让更多的包装进袋子里面,那么就要选择一个没有重复数字的递增序列,比如1,2,3,4就是符合要求的。2,3,4也是。那么重复数字最多的个数就是最后剩下的包数。
但是这样提交的WA了。 思路是没错,就是输出有错,这个不知道为什么,个人觉得这样输出也应该是可以的,题目都说了有多种方案,任何一种都是可以的...
最后看了下网上的输出方法,每次以最多重复数字个数为等差距离输出方案。




【代码】
/*
 *  UVa: 11100 - The Trip, 2007
 *  Time: 0.112s
 *  Author: D_Double
 *
 */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 20000
using namespace std;
int arr[MAXN], n, ans[MAXN];
bool vis[MAXN];

void solve(){
    sort(arr, arr+n);
    int Max=1;
    int i=1;
    while(i<n){
        int cnt=1;
        while(arr[i]==arr[i-1]&&i<n){ ++cnt; ++i;}
        if(cnt>Max) Max=cnt;
        ++i;
    }
    printf("%d\n", Max);
    memset(vis, 0, sizeof(vis));
    i=0;

    while(1){
        for(i=0; i<n; ++i)if(!vis[i])break;
        printf("%d",arr[i]);
        vis[i] = true;
        i += Max;
        while(i<n){
            if(!vis[i]){
                printf(" %d",arr[i]);
                vis[i] = true;
            }
            i += Max;
        }
        printf("\n");
        bool flag=false;
        for(int i=0; i<n; ++i)if(!vis[i]){
            flag=true; break;
        }
        if(!flag) break;
    }
}

int main(){
    bool flag=false;
    while(scanf("%d",&n)&&n){
        for(int i=0; i<n; ++i)
            scanf("%d",&arr[i]);
        if(flag) printf("\n");
        else flag=true;
        solve(); 
    }
    return 0;
}


——  生命的意义,在于赋予它意义。

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)






你可能感兴趣的:(UVa 11100 - The Trip, 2007)