您的位置:首页 > 其它

UVa 11100 - The Trip, 2007

2013-08-21 19:59 369 查看
  题目大意:有n个包,其中小包可以装到大的包里,包的大小用数字进行表示,求最小的装包数量。

  统计每个数字出现的次数,其中次数的最大值k就是最小的装包数量。答案显然不可能比k小,而把相同大小的包装进不同的大包里显然满足条件。题目中有一句话纠结了半天,“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.” 大概是说让每个大包里的小包的数量的最大值最小吧,也就是要把小包尽可能平均放到这k个大包里,于是就有了有了“排序,以k为等差的序列”这一解法,因为a[i+k] > a[i]。

#include <cstdio>
#include <algorithm>
using namespace std;
#define MAXN 10000+10

int a[MAXN];

int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int n;
bool first = true;
while (scanf("%d", &n) != EOF && n)
{
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
sort(a, a+n);
int k = 1, cnt = 1;
for (int i = 1; i <= n; i++)
{
if (i == n || a[i] != a[i-1])
{
if (cnt > k)  k = cnt;
cnt = 1;
}
else  cnt++;
}
if (first)  first = false;
else  printf("\n");
printf("%d\n", k);
for (int i = 0; i < k; i++)
{
for (int j = i; j < n; j += k)
printf("%s%d", (i==j) ? "" : " ", a[j]);
printf("\n");
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: