uva 11100 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 ,以及各个袋子的大小,你可以将小袋子装在大袋子里面,使得露在外面的袋子的总数量最少?以及各个袋子的情况


解题思路:

这题弹了很多遍,原因算法不是很正确。尝试了第三种想法,才AC。

首先袋子的数量最多的那种大小一定决定了最终的答案,关卡点,也就是那个数量。

然后排好序依次 放。


样例解释:2号大小的数量(3个)最多,因此答案是3

路径的话:排好序还是 1 1 2 2 2 3

第一次放完:

1


第二次放完

1

1


第三次

1

1

2


第四次

1  2

1

2


第五次

1  2

1  2

2


第六次

1   2

1   2

2  3


结束,输出答案




#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

int ans,n;
vector <vector<int> > v;
vector <int> data;
map <int,int> mp;

void input(){
    ans=0;
    mp.clear();v.clear();data.clear();
    for(int i=0;i<n;i++){
        int x;
        scanf("%d",&x);
        data.push_back(x);
        mp[x]++;
        if(mp[x]>ans) ans=mp[x];
    }
    sort(data.begin(),data.end());
    v.resize(ans);
}

void computing(){
    printf("%d\n",ans);
    int pos=0,now=0;
    while(pos<n){
        if(now>=ans) now-=ans;
        if(v[now].size()<=0 || v[now].back()<data[pos]){
            v[now].push_back(data[pos]);
            pos++;
        }
        now++;
    }
    for(int i=0;i<ans;i++){
        printf("%d",v[i][0]);
        for(int j=1;j<v[i].size();j++){
            printf(" %d",v[i][j]);
        }
        printf("\n");
    }
}

int main(){
    int casen=0;
    while(scanf("%d",&n)!=EOF && n ){
        input();
        if(casen>0) printf("\n");
        computing();
        casen++;
    }
    return 0;
}





posted @ 2014-03-12 12:06  炒饭君  阅读(241)  评论(0编辑  收藏  举报