UVa 11100 The Trip, 2007 (贪心&一举两得的输出技巧)
来源:互联网 发布:mysql表空间查看 编辑:程序博客网 时间:2024/06/14 18:27
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
61 1 2 2 2 30
Output for Sample Input
31 21 23 2
学英语:
each giving the dimensions of the bags comprising one piece.
每行输出一组包中所有包的规格。
贪心思路:最终包的个数k取决于相同规格最多的包的数目(样例中2最多,那k就是2的个数——3)
但是题目又要求每组包的数目最小,怎么输出呢?——排序后,间隔k输出即可,因为k是出现最多的数,所以每隔k个输出保证不会相同,同时每组包的数目又最小。
完整代码:
/*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;}
- UVa 11100 The Trip, 2007 (贪心&一举两得的输出技巧)
- UVA 11100 The Trip, 2007 贪心(输出比较奇葩)
- UVA 11100 The Trip, 2007 (贪心)
- uva 11100 The Trip, 2007(贪心)
- UVA 11100 - The Trip, 2007(贪心)
- uva - 11100 - The Trip, 2007(贪心)
- UVA - 11100 The Trip, 2007(贪心)
- UVA - 11100 The Trip, 2007 贪心
- UVA 11100 The Trip,2007(贪心)
- UVA 111100 The Trip, 2007 贪心 *
- UVA 11100 The Trip, 2007
- UVa 11100 - The Trip, 2007
- UVa 11100 - The Trip, 2007
- uva 11100 - The Trip, 2007
- uva 11100 - The Trip, 2007
- UVA 11100 - The Trip, 2007
- uva 11100 The Trip, 2007
- UVA 11100 - The Trip, 2007
- IT圈子里有个圈子—程序员
- 黑马程序员_网络编程
- 各种排序算法稳定性的总结
- 入侵oracle数据库时常用的操作命令整理
- #pragma pack()
- UVa 11100 The Trip, 2007 (贪心&一举两得的输出技巧)
- (1)字符串全排列 / 组合
- 手机界面布局
- 详解Big-Endian和Little-Endian,大端模式和小端模式
- systemui statusbar service 小结
- C++宏定义错误
- 游戏嵌入 文字广告"欢迎使用**组系列游戏,**组网站致力打造国内最大最全资源分享平台!"
- Oracle 10g 下载地址
- HTML 常用标记