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

题意:给出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;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值