UVa 11100 The Trip, 2007:贪心&一举两得的输出技巧

11100 - The Trip, 2007

Time limit: 3.000 seconds

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

学英语:

each giving the dimensions of the bags comprising one piece.

每行输出一组包中所有包的规格。

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

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

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

完整代码:

/*0.042s*/

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;  

int a[10005], num[1000005];  

int main()
{
    int n, k, i, j;
    while (scanf("%d", &n), n)
    {
        memset(num, 0, sizeof(num));
        k = 0;
        for (i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
            ++num[a[i]];
            k = max(k, num[a[i]]);///一样大小的包最多有多少~
        }
        sort(a, a + n);
        printf("%d\n", k);
        for (i = 0; i < k; i++)
        {
            printf("%d", a[i]);
            for (j = i + k; j < n; j += k)
                ///j+=k,这样输出保证每组包的大小必互不相同,同时保证了每组包的数目最小(you are also to minimize the total number of bags in any one piece that must be carried.)
                printf(" %d", a[j]);
            putchar(10);
        }
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索贪心法 最小圈基问题
, 贪心
, number
, 输出
, 数目
, of
, The
, bag
One
a trip to the moon、on the trip、the trip、the trip to egypt、trip the light,以便于您获取更多的相关知识。

时间: 2024-10-19 19:21:10

UVa 11100 The Trip, 2007:贪心&amp;一举两得的输出技巧的相关文章

uva 11100 - The Trip, 2007

点击打开链接uva 11100 题目意思: 给定n个包,现在每一个包的形状相同,但是大小不同.现在规定小号的包可以包含在大号里面.例如 4-3-2-1,现在给我们n个包,要我们求出最后需要的包是几个,还有尽量满足每一个最后包之间包含的小包的个数相同 解题思路: 1:思路:贪心 2:分析如下:       1首先我们知道相同大小的包是不能够包含在同一个大包里面的,所以最后需要的大包的个数就是原来序列中相同型号包的最大值           2 知道了最后需要的包的个数,那么就可以根据总的包个数n求

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

Word 2007选取文档内容的技巧 及如何去掉修改标记

&http://www.aliyun.com/zixun/aggregation/37954.html">nbsp;   Microsoft Word在当前使用中是占有巨大优势的文字处理器,这使得Word专用的档案格式Word 文件(.doc)成为事实上最通用的标准.Word文件格式的详细资料并不对外公开.Word文件格式不只一种,因为随Word软件本身的更新,文件格式也会或多或少的改版,新版的格式不一定能被旧版的程序读取(大致上是因为旧版并未内建支援新版格式的能力).微软已经详细

UVa 10718 Bit Mask:贪心&amp;amp;位运算

10718 - Bit Mask Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1659 In bit-wise expression, mask is a common term. You can get a certain bit-pattern using mask. For exa

UVa 10020 Minimal coverage:贪心&amp;amp;区间覆盖

10020 - Minimal coverage Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=961 The Problem Given several segments of line (int the X axis) with coordinates

UVa 10137 The Trip:小数四舍五入&amp;amp;需要注意的地方

10137 - The Trip Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=29&page=show_problem&problem=1078 A number of students are members of a club that travels annually to exotic locations

UVa 12261 High Score:贪心&amp;amp;“向左走,向右走”

12261 - High Score Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=onlinejudge&Itemid=99999999&category=244&page=show_problem&problem=3413 思路:注意名字中有若干个A的情况,这时对每串连续的A进行"向左走,向右走"的判断,取所有情况中的最小值输出即可. 完整代码: /*0.0

uva 10137 The trip

/* The trip 注意特殊数据的处理,误差不超过0.01即可. */#include<iostream> #include<cstdio> using namespace std; double a[1005]; int main() { // freopen("./pcio/110103.inp","r",stdin); int n,i; while(~scanf("%d",&n)) { if(n==0)

UVa 400 Unix ls:字符串排序&amp;amp;规格化输出

400 - Unix ls Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=341 The computer company you work for is introducing a brand new computer line and is develo