Problem A: The Trip, 2007
A number of students are members of a club that travels annually to exotic locations. Their destinations in the past have included Indianapolis, Phoenix, Nashville, Philadelphia, San Jose, Atlanta, Eindhoven, Orlando, Vancouver, Honolulu, Beverly Hills, Prague, Shanghai, and San Antonio. This spring they are hoping to make a similar trip but aren't quite sure where or when.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 n integers 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.
Sample Input
6 1 1 2 2 2 3 0
Output for Sample Input
3 1 2 1 2 3 2
解题报告:先看清题意。给定一组数,将其分成尽量少的严格递增的序列。且使每个序列内的数的个数最少。
简单思考一下,可以得出结论:相同数字有多少个,序列最少就有多少个。
当求出序列有多少个后,我们可以求出每个序列有多少个数字。假设序列数为p,那么每个序列有n/p个数字。当然,n/p可能不是整数,在这种情况下最后一个序列内数字的数量就是1到(n-1)/p+1个了。而题目要求我们使每个序列的数尽量少,在这种情况下我们可以将其他序列里的数放进最后一个序列里。
总的策略就是每次选择当前重复数最多的数字。代码如下:(C++ 11 提交)
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <map> #include <string> using namespace std; #define ff(i, n) for(int i=0;i<(n);i++) #define fff(i, n, m) for(int i=(n);i<=(m);i++) #define dff(i, n, m) for(int i=(n);i>=(m);i--) typedef long long LL; typedef unsigned long long ULL; void work(); int main() { #ifdef ACM freopen("in.txt", "r", stdin); #endif // ACM work(); } /// struct Node { int a; int num; bool operator<(const Node & cmp) const { return num < cmp.num; } } x; int n; void check(int p, priority_queue<Node> & que) { int e = (n-1)/p+1; int sta = p-(e*p-n); ff(i, p) { if(i==sta) e--; vector<Node> v; bool first = true; ff(j, e) { if(que.size()==0) break; x = que.top(); que.pop(); x.num--; v.push_back(x); if(first) first=false; else printf(" "); printf("%d", x); } puts(""); ff(j, v.size()) if(v[j].num) que.push(v[j]); } } void work() { while(~scanf("%d", &n) && n) { map<int, int> m; int tmp; ff(i, n) scanf("%d", &tmp), m[tmp]++; priority_queue<Node> que; int l = 0; for(auto i : m) x.a=i.first, x.num=i.second, l=max(l, i.second), que.push(x); printf("%d\n", l); check(l, que); puts(""); } }