题目链接: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个输出保证不会相同,同时每组包的数目又最小

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
bool cmp(int x,int y)
{
return x>y;
}
const int N=;
const int mod=1e9+;
int a[N], num[];
int main()
{
int n, k, i, j;
while (scanf("%d", &n), n){
mem(num);
k = ;
for (i = ; 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 = ; i < k; i++){
printf("%d", a[i]);
for (j = i + k; j < n; j += k)
printf(" %d", a[j]);
putchar();
}
}
return ;
}

UVA 11100 The Trip, 2007 (贪心)的更多相关文章

  1. UVA 11100 The Trip, 2007 贪心(输出比较奇葩)

    题意:给出n个包的大小,规定一个大包能装一个小包,问最少能装成几个包. 只要排序,然后取连续出现次数最多的数的那个次数.输出注意需要等距输出. 代码: /* * Author: illuz <i ...

  2. UVA 11100 The Trip, 2007 水题一枚

    题目链接:UVA - 11100 题意描述:n个旅行箱,形状相同,尺寸不同,尺寸小的可以放在尺寸大的旅行箱里.现在要求露在最外面的旅行箱的数量最少的同时满足一个旅行箱里放的旅行箱的数量最少.求出这样满 ...

  3. UVa 11100 The Trip, 2007 (题意+贪心)

    题意:有n个包,其中小包可以装到大的包里,包的大小用数字进行表示,求最小的装包数量. 析:这个题的题意不太好理解,主要是有一句话难懂,意思是让每个最大包里的小包数量的最大值尽量小,所以我们就不能随便输 ...

  4. UVa 11100 - The Trip, 2007 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  5. UVa 11100 The Trip, 2007

    今天的教训:做题要用大块的时间来做,上午做一下,做题做到一半就去忙别的事,那么后面再做的时候就无限CE,WA了.因为你很难或者需要很长时间来找回当时的思路. 题意:就像套瓷娃娃一样,有n个包,大小可能 ...

  6. The trip(Uva 11100)

    题目大意: 给出n个数,要求将其分成最少的递增序列,保证序列最少的同时要使得序列长度的最大值最小.  n<=10000 题解: 1.我是直接看着<训练指南>里的中文题面的,lrj似乎 ...

  7. UVa 11100 旅行2007

    https://vjudge.net/problem/UVA-11100 题意: 给定n个正整数,把它们划分成尽量少的严格递增序列,尽量均分. 思路: 因为必须严格递增,所以先统计每个数字出现的次数, ...

  8. 【NOIP合并果子】uva 10954 add all【贪心】——yhx

    Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvesconde ...

  9. uva 11134 fabled rooks (贪心)——yhx

    We would like to place n rooks, 1 n 5000, on a n nboard subject to the following restrictions• The i ...

随机推荐

  1. 算法面经之讯飞+CVTE

    一.科大讯飞(合肥) 概况:刚经历了科大讯飞的初面,大概35分钟左右,问的内容比较笼统,主要针对简历上的内容来,面试官比较亲切,回忆了一下面试内容. 建议:把简历上的内容整吧清楚,不知道的别瞎写,写了 ...

  2. vue 后退不刷新页面

    使用 this.$router.push({path: '/aichat'})路由跳转方式跳转页面 要实现 home => chat  chat页面刷新: chat => report, ...

  3. 深入理解Lua的闭包一:概念、应用和实现原理

    本文首先通过具体的例子讲解了Lua中闭包的概念,然后总结了闭包的应用场合,最后探讨了Lua中闭包的实现原理.   闭包的概念 在Lua中,闭包(closure)是由一个函数和该函数会访问到的非局部变量 ...

  4. jenkins借助winscp传本地文件到远程服务器上

    有这样的场景,我们的ftp上都是些重要的资料,所以大家基本只有可看的权限,只有部分管理人员有可读可写的权限,但是jenkins上基本使用的都是ftp的路径,这个时候就存在一些问题,某些开发需要将自己构 ...

  5. [LeetCode] 98. Validate Binary Search Tree_Medium

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. for in //for of //forEach //map三种对比

    遍历Array可以采用下标循环,遍历Map和Set就无法使用下标.为了统一集合类型,ES6标准引入了新的iterable类型,Array.Map和Set都属于iterable类型. 具有iterabl ...

  7. shell编程:case语句

  8. bat运行时自己隐藏黑框,而不是用vbs来调用自己

    //autoStart.bat @echo off if "%1" == "h" goto begin mshta vbscript:createobject( ...

  9. POJ 2155 Matrix(二维BIT)

    Matrix [题目链接]Matrix [题目类型]二维BIT &题解: bit只能单点更新,恰好,这题可以想一下就可以用单点更新解决了. 只不过最后我交上去居然T了,想了10多分钟,试了一下 ...

  10. Hibernate.编写xml文件无自动提示信息

    Hibernate.编写xml文件无自动提示信息 注意: 配置 xxxx.hbm.xml 文件的自动提示.和配置 hibernate.cfg.xml 文件的提示,操作步骤是一样的.只是复制的文件内容. ...