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 nintegers 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个包裹。每个包裹有一个数字。。数字小的能套在数字大的里面。现在要求出一个嵌套方式使得最终套出来的总包裹数最少。。
思路: 贪心。想一下。。发现每个包裹,只要和他大小不同的。就能进行嵌套。。这是肯定的。除非遇到大小相同。才不能嵌套。。这么一想。能套出的最小包裹数。就等于包裹大小相同数量最大的包裹的数量。因为包裹相同不能套。那么这几个包裹肯定得分开来了。而其他包裹又没有这个包裹多。肯定能和这些包裹进行嵌套。。
所以该问题转化为求出数量最多的包裹。输出嵌套方式。。这里嵌套方式的输出方法。。比如样例。数量有6个。最多数量的包裹大小为2,数量为3.那么可以套出3种。这样每种情况会套6 / 3个包裹。。因为之前是有排过序的,所以就按间隔为2输出即可。。
代码:
#include <stdio.h>
#include <algorithm>
using namespace std;

int n, num[10005], Max, he;
int main() {
    int bo = 0;
    while (~scanf("%d", &n) && n) {
	if (bo == 0)
	    bo = 1;
	else
	    printf("\n");
	Max = 1; he = 1;
	for (int i = 0; i < n; i ++)
	    scanf("%d", &num[i]);
	sort(num, num + n);
	for (int i = 0; i < n; i ++) {
	    if (num[i] == num[i + 1]) {
		he ++;
	    }
	    else {
		if (Max < he) {
		    Max = he;
		}
		he = 1;
	    }
	}
	printf("%d\n", Max);
	for (int i = 0; i < Max; i ++) {
	    for (int j = i; j < n; j += Max) {
		printf("%d", num[j]);
		if (j + Max < n) printf(" ");
		else printf("\n");
	    }
	}
    }
    return 0;
}




你可能感兴趣的:(UVA 11100 The Trip, 2007 (贪心))