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

时间:2022-01-27 20:12:47

题意:给出n个包的大小,规定一个大包能装一个小包,问最少能装成几个包。

只要排序,然后取连续出现次数最多的数的那个次数。输出注意需要等距输出。

代码:

 /*
* Author: illuz <iilluzen[at]gmail.com>
* Blog: http://blog.csdn.net/hcbbt
* File: uva11100.cpp
* Lauguage: C/C++
* Create Date: 2013-09-03 16:25:50
* Descripton: uva11100, The Trip, greed
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ms(a, i, n) memset(a, i, sizeof(a[0]) * (n)) const int MAXN = 1000100;
int n, a[MAXN], Max, cnt, cas;
bool flag, vis[MAXN]; int main() {
cas = 0;
while (scanf("%d", &n) && n) {
rep(i, n) scanf("%d", &a[i]);
sort(a, a + n);
Max = cnt = 1;
rep(i, n - 1) {
if (a[i] == a[i + 1]) cnt++;
else cnt = 1;
if (Max < cnt) Max = cnt;
}
ms(vis, 0, n);
if (cas) printf("\n");
printf("%d\n", Max);
int i;
cnt = 0;
while (1) {
if (cnt >= n) break;
for (i = 0; i < n; i++)
if( !vis[i]) break;
cnt++;
vis[i] = true;
printf("%d", a[i]);
i += Max;
for (; i < n; i += Max)
if (!vis[i]) {
printf(" %d", a[i]);
vis[i] = true;
cnt++;
}
printf("\n");
}
}
return 0;
}