uva 11100 - The Trip, 2007(贪心)类似于一道LIS题目

来源:互联网 发布:数据搜索网站 编辑:程序博客网 时间:2024/06/13 00:23

1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2041

2、题目大意:

有很多包,已知包的区别只在于高度不一样,小包可以装到大包里边,求最终剩下多少个包,并输出没一个组合

3、实际上题目中要求的最终剩下多少个包,就是要求相同的包的最大数,因为这些包只能单独作为一个包,其他的都可以嵌套在这些包中

4、题目:

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 integer1 ≤ 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 ofk, 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

61 1 2 2 2 30

Output for Sample Input

31 21 23 2
4、ac代码:

#include<stdio.h>#include<algorithm>using namespace std;#define N 10005int a[N];int cmp(int a,int b){    return a<b;}int main(){    int n,cas=0;    while(scanf("%d",&n))    {        cas++;        if(n==0)            break;        if(cas!=1)            printf("\n");        for(int i=1; i<=n; i++)        {            scanf("%d",&a[i]);        }        sort(a+1,a+n+1,cmp);        int temp=a[1];        int count=1;        int MAX=-1;        for(int i=2; i<=n; i++)        {            if(a[i]==temp)                count++;            else            {                temp=a[i];                count=1;            }            if(count>MAX)                MAX=count;        }        printf("%d\n",MAX);        int i=1;        for(int i=1; i<=MAX; i++)        {            int flag=0;            for(int j=n-i+1; j>=1; j-=MAX)            {                if(flag==0)                {                    printf("%d",a[j]);                    flag=1;                }                else                {                    printf(" %d",a[j]);                }            }            printf("\n");        }    }    return 0;}/*61 1 2 2 2 361 1 2 2 2 30*/

用LIS做的wrong代码,待解决

#include<stdio.h>#include<string.h>#define N 10005#define Max 1000005#include<algorithm>using namespace std;long long stack[N];long long dp[N];int a[N];int b[N];int visit[1000005];long long maxx;int cmp(int a,int b){    return a<b;}void LIS(int n){    int top=0;    long long sum=0;    memset(stack,0,sizeof(stack));    stack[top]=-1;    maxx=-1;    for(int i=1; i<=n; i++)    {        if(a[i]>stack[top])        {            stack[++top]=a[i];            dp[i]=top;        }        else        {            int l=1,r=top;            while(l<=r)            {                int mid=(l+r)>>1;                if(a[i]<stack[mid])                    r=mid-1;                else                    l=mid+1;            }            stack[r]=a[i];            dp[i]=r;            //printf("l=%d\n",r);        }        if(dp[i]>maxx)        {            maxx=dp[i];            memset(b,0,sizeof(b));            for(int j=1; j<=dp[i]; j++)                b[j]=stack[j];        }    }}int main(){    int n;    int cas=0;    while(scanf("%d",&n))    {        cas++;        if(n==0)            break;        for(int i=1; i<=n; i++)        {            scanf("%d",&a[i]);        }        sort(a+1,a+1+n,cmp);        LIS(n);        if(cas!=1)            printf("\n");        printf("%lld\n",maxx);        for(int i=1; i<=maxx; i++)        {            memset(visit,0,sizeof(visit));            for(int j=1; j<=b[i]; j++)            {                if(a[j]<b[i]&&visit[a[j]]==0)                {                    visit[a[j]]=1;                    printf("%d ",a[j]);                    a[j]=Max;                }            }            printf("%d\n",b[i]);        }    }    return 0;}



原创粉丝点击