• General Problem Solving Techniques [Intermediate-1]~E


    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"stdio.h"
    #include"string.h"
    #include"algorithm"
    using namespace std;
    const int N=10010;
    const int M=1000010;
    int a[N];
    int num[M];
    int main()
    {
        int n,i,j,counta;
        while(scanf("%d",&n)&&n)
        {
            counta=0;
            memset(num,0,sizeof(num));
            for(i=0;i<n;i++)
            {
                scanf("%d",&a[i]);
                num[a[i]]++;
                counta=max(counta,num[a[i]]);
            }
            sort(a,a+n);
            printf("%d
    ",counta);
            for(i=0;i<counta;i++)
            {
                printf("%d",a[i]);
                for(j=i+counta;j<n;j+=counta)
                    printf(" %d",a[j]);
                printf("
    ");
            }
        }
        return 0;
    }
    
     
     
  • 相关阅读:
    [Sql Server][原创]
    SQL Server T-SQL高级查询
    SQL 网文链接
    Epicor系统二次开发
    lambda表达式的变量作用域
    写一个正则表达式匹配手机号
    函数装饰器在类方法中的使用方法
    关于Django的session的使用 (装饰器版)
    Django ORM相关操作(2)
    Django ORM相关操作(1)
  • 原文地址:https://www.cnblogs.com/chenchunhui/p/4864944.html
Copyright © 2020-2023  润新知