UVa 11100 The Trip, 2007: The output skill of greed & kill both birds

Source: Internet
Author: User
Tags pack printf time limit

11100-the Trip, 2007

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=2041

A number of students are members of the 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 trips but aren ' t quite sure where or when.

A issue with the "trip is" their very generous sponsors always give them various-knapsacks and other carrying bags tha T 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 s O 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 are a positive integer not Excee Ding 1000000. A bag with smaller dimension would fit into one with larger dimension. You are are to compute which bags to pack within which others-as to minimize the overall number of pieces of luggage (i.e. The number of outermost bags). While maintaining the minimal number of pieces for you are also to minimize the total number of bags into any one one piece that Mus t be carried.

Standard input contains several test cases. Each test case consists of a integer1≤n≤10000 giving the number of bags followed byn integers on one or more lines, E Ach giving the dimension of a piece. A line containing 0 follows of 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 dim Ensions of the bags comprising one piece, separated by spaces. Each dimension in the input should appear exactly once in the output, and the bags at each piece must fit nested one Withi N another. If there is more than one solution, the any would 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

Learn English:

Each giving the dimensions of the bags comprising one piece.

The specifications of all packages in a set of packages are output per line.

Greedy idea: The number of the final package K depends on the number of packages with the most of the same specification (2 in the sample, the K is the number of 2--3)

But the title also requires the smallest number of packages per package, how to output it? --After sorting, the interval K output can be, because K is the largest number, so every k output guarantee will not be the same, and the number of packets per package is the smallest.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Complete code:

/*0.042s*/
    
#include <cstdio>  
#include <cstring>  
#include <algorithm>  
using namespace Std;  
    
int a[10005], num[1000005];  
    
int main ()  
{  
    int n, K, I, J;  
    while (scanf ("%d", &n), N)  
    {  
        memset (num, 0, sizeof (num));  
        k = 0;  
        for (i = 0; i < n; i++)  
        {  
            scanf ("%d", &a[i]);  
            ++num[a[i]];  
            K = max (k, num[a[i]);///the size of a packet with a maximum number of ~  
        }  
        Sort (A, a + N);  
        printf ("%d\n", k);  
        for (i = 0; i < K; i++)  
        {  
            printf ("%d", A[i]);  
            for (j = i + K; j < n; j = k)  
                ///j+=k so that the output guarantees that each package will be of the same size, while ensuring that the number of packets per package is minimal (you are also to minimize F bags in any one piece that must to be carried.)  
                printf ("%d", a[j]);  
            Putchar (ten);  
        }  
    }  
    return 0;  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.