UVA 11100 The Trip, 2007 (贪心)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&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 integer1 ≤ n ≤ 10000 giving the number of bags followed byn 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 klines, 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

贪心思路:最终包的个数k取决于相同规格最多的包的数目(样例中2最多,那k就是2的个数——3)

但是题目又要求每组包的数目最小,怎么输出呢?——排序后,间隔k输出即可,因为k是出现最多的数,所以每隔k个输出保证不会相同,同时每组包的数目又最小

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <vector>
 6 #include <cstdlib>
 7 #include <iomanip>
 8 #include <cmath>
 9 #include <ctime>
10 #include <map>
11 #include <set>
12 #include <queue>
13 using namespace std;
14 #define lowbit(x) (x&(-x))
15 #define max(x,y) (x>y?x:y)
16 #define min(x,y) (x<y?x:y)
17 #define MAX 100000000000000000
18 #define MOD 1000000007
19 #define pi acos(-1.0)
20 #define ei exp(1)
21 #define PI 3.141592653589793238462
22 #define INF 0x3f3f3f3f3f
23 #define mem(a) (memset(a,0,sizeof(a)))
24 typedef long long ll;
25 ll gcd(ll a,ll b){
26     return b?gcd(b,a%b):a;
27 }
28 bool cmp(int x,int y)
29 {
30     return x>y;
31 }
32 const int N=10005;
33 const int mod=1e9+7;
34 int a[N], num[1000005];
35 int main()
36 {
37     int n, k, i, j;
38     while (scanf("%d", &n), n){
39         mem(num);
40         k = 0;
41         for (i = 0; i < n; i++){
42             scanf("%d", &a[i]);
43             ++num[a[i]];
44             k = max(k, num[a[i]]);
45         }
46         sort(a, a + n);
47         printf("%d\n", k);
48         for (i = 0; i < k; i++){
49             printf("%d", a[i]);
50             for (j = i + k; j < n; j += k)
51                 printf(" %d", a[j]);
52             putchar(10);
53         }
54     }
55     return 0;
56 }
posted @ 2017-08-04 10:48  wydxry  阅读(340)  评论(0编辑  收藏  举报
Live2D