博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
General Problem Solving Techniques [Intermediate-1]~E - The Trip, 2007
阅读量:5050 次
发布时间:2019-06-12

本文共 2426 字,大约阅读时间需要 8 分钟。

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.
Input
Standard input contains several test cases. Each test case consists of an integer 1 ≤ n ≤ 10000 giving
the number of bags followed by n integers on one or more lines, each giving the dimension of a piece.
A line containing 0 follows the last test case.
Output
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
Sample Output
3
1 2
1 2
3 2

解题思路:题目的大概意思是将给出的正整数尽量少的分割成严格递增的规格。可以用一个数组记录出每个数字出现的次数,求出出现次数最多的数字的个数,这里有一个技巧,可以利用周期来解答。求出出现次数最多的数出来之后,可以利用周期,因为要求每一组中一个数只能出现一次,利用出现次数最多的那个数的个数作为周期,那么一个组中就不可能出现两个相同的数了。利用这一特点,做这个题目就简单好多。

程序代码:

#include"stdio.h"#include"string.h"#include"algorithm" using namespace std; const int N=10010; const int M=1000010; int a[N]; int num[M]; int main() { int n,i,j,counta; while(scanf("%d",&n)&&n) { counta=0; memset(num,0,sizeof(num)); for(i=0;i
 
 

转载于:https://www.cnblogs.com/chenchunhui/p/4864944.html

你可能感兴趣的文章
oracle数据类型
查看>>
socket
查看>>
Vue中使用key的作用
查看>>
二叉索引树 树状数组
查看>>
日志框架--(一)基础篇
查看>>
Java设计模式之原型模式
查看>>
Spring学习(四)-----Spring Bean引用同xml和不同xml bean的例子
查看>>
哲理故事与管理之道(20)-用危机激励下属
查看>>
关于源程序到可运行程序的过程
查看>>
wepy的使用
查看>>
转载:mysql数据库密码忘记找回方法
查看>>
scratch少儿编程第一季——06、人在江湖混,没有背景怎么行。
查看>>
面向对象1
查看>>
在ns2.35中添加myevalvid框架
查看>>
【贪心+DFS】D. Field expansion
查看>>
为什么要使用href=”javascript:void(0);”
查看>>
二进制文件的查看和编辑
查看>>
C# Async与Await的使用
查看>>
Mysql性能调优
查看>>
iOS基础-UIKit框架-多控制器管理-实例:qq界面框架
查看>>