现在的位置: 首页 > 综合 > 正文

UVA 11100 – The Trip, 2007(贪心)

2014年10月31日 ⁄ 综合 ⁄ 共 2120字 ⁄ 字号 评论关闭

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个包,大包能套小包,问最多能套成几个。

思路:贪心,数量最多的包肯定作为基准个数,然后去套即可。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;

const int N = 10005;

int n, num[N], Max;
map<int, int> vis;

void init() {
    Max = 0;
    vis.clear();
    for (int i = 0; i < n; i ++) {
	scanf("%d", &num[i]);
	vis[num[i]] ++;
	Max = max(vis[num[i]], Max);
    }
    sort(num, num + n);
}

void solve() {
    printf("%d\n", Max);
    for (int i = 0; i < Max; i ++) {
	printf("%d", num[i]);
	for (int j = i + Max; j < n; j += Max)
	    printf(" %d", num[j]);
	printf("\n");
    }
}

int main() {
    while (~scanf("%d", &n) && n) {
	init();
	solve();
    }
    return 0;
}

抱歉!评论已关闭.