zoukankan      html  css  js  c++  java
  • 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;
    }
    
     
     
  • 相关阅读:
    python基础学习Day15 面向对象、类名称空间、对象名称空间 (2)
    python基础学习Day14 内置函数 匿名函数
    python基础学习Day12 生成器、列表推导式、字典的表达式、字典键值对的互换、集合推导式
    python基础学习Day11 函数名的应用、闭包、迭代器
    python基础学习Day10 函数形参的动态参数、*args **kwargs 命名空间 global 与 nonlocal
    python基础学习Day9 函数的初识,实参、形参、
    定时器定时循环执行和只执行一次
    CocoaPods 安装和使用
    tableView的cell之间间隔显示空白区域
    去掉tableView空白区域的分割线
  • 原文地址:https://www.cnblogs.com/chenchunhui/p/4864944.html
Copyright © 2011-2022 走看看