您的位置:首页 > 其它

uva 11100 The Trip, 2007

2016-04-02 11:09 411 查看
原题:

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.

Input

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.

Output

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

Sample Output

3

1 2

1 2

3 2

大意:

给你数,现在让你分成尽量少的严格递增序列。先输出序列个数的最小值,然后再数出这些序列。

#include <bits/stdc++.h>
using namespace std;
int a[1000001],b[10001];
vector<int> vi;
int main()
{
ios::sync_with_stdio(false);
int n,ans,tmp;
while(cin>>n,n)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
vi.clear();
ans=1;
cin>>b[1];
tmp=b[1];
a[b[1]]++;
for(int i=2;i<=n;i++)
{
cin>>b[i];
a[b[i]]++;
if(a[b[i]]>ans)
ans=a[b[i]];
}
cout<<ans<<endl;
sort(b+1,b+1+n);
for(int i=1;i<=ans;i++)
{
vi.clear();
for(int j=i;j<=n;j=j+ans)
vi.push_back(b[j]);
for(int k=0;k<vi.size();k++)
{
if(k!=vi.size()-1)
cout<<vi[k]<<" ";
else
cout<<vi[k]<<endl;
}
}
}
return 0;
}


解答:

这题让输出的第一个解很好想,但是输出方案想多了,后来看了一眼别人的代码,瞬间懂了。

最少分成的组数,而且还要能保持输出的序列是严格递增的,那么最少的个数一定是这个数组里面出现次数最多的那个数字的个数,答案是ans。然后每次以ans为跨度进行选择数,然后输出方案即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: