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 integer 1 ≤ n ≤ 10000 giving the number of bags followed by nintegers 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 k lines, 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.
6 1 1 2 2 2 3 0
3 1 2 1 23 2
给很多个包的大小,小的可以装到大的里面去,问怎样装可以使最多的组里的包尽量的少。
首先相同的数字最多的就是组数,然后按照这个为距离输出可以保证是合理一组,且尽量分散。
#include<iostream> #include<algorithm> #include<cmath> #include<cstdio> #include<string> #include<cstring> #include<cstdlib> #include<queue> #include<vector> #include<functional> #include<stack> using namespace std; const int maxn = 10005; int n, a[maxn], f[maxn], k, minn, ff = 0; int main(){ while (cin >> n, n) { if (ff) printf("\n"); else ff = 1; for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); k = -1; memset(f, -1, sizeof(f)); minn = -1; for (int i = 0; i < n - 1; i++) if (a[i] == a[i + 1]) minn = min(--k, minn); else k = -1; k = -minn; printf("%d\n", k); for (int i = 0; i < k; i++) { printf("%d", a[i]); for (int j = i + k; j < n; j += k) printf(" %d", a[j]); printf("\n"); } } return 0; }