题意:n个包,大尺寸的包里可以放小尺寸的包,问最后最少剩余几个包(嵌套起来算一个), 把包的嵌套情况也输出出来。
题解:每种尺寸数量最多的一定最后剩余几个包的数量m,然后排序,每隔m就输出一次,刚好把所有匹配情况都能输出出来。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 10005;
const int MAX = 1000000;
int n, bag[N], vis[MAX], pie[N], maxx;
int main() {
int t = 1;
while (scanf("%d", &n) && n) {
maxx = -1;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++) {
scanf("%d", &bag[i]);
vis[bag[i]]++;
if (vis[bag[i]] > maxx)
maxx = vis[bag[i]];
}
sort(bag, bag + n);
if (t != 1)
printf("\n");
printf("%d\n", maxx);
for (int i = 0; i < maxx; i++) {
printf("%d", bag[i]);
for (int j = i + maxx; j < n; j += maxx)
printf(" %d", bag[j]);
printf("\n");
}
t++;
}
return 0;
}