uva 10131 越大越聪明

来源:互联网 发布:手机网络被屏蔽怎么办 编辑:程序博客网 时间:2024/05/20 18:03

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

2、题目大意:

给您定很多象的重量和智商IQ值,现在要找出重量从小到大排序,智商IQ值从大小顺序的最长序列

首先将象按照体重从小到大排序,然后求IQ的递减序列,开始用的LIS求的,不对,后来看网上有一种很简单的代码:

定义状态dp[i]表示以当前大象为终点形成的最长序列长度

状态dp[j]表示i之前的符合要求的大象为终点的最长的序列长度

那么有状态转移方程,if(dp[i]<dp[j]+1) ,那么dp[i]=dp[j]+1;

题目要求输出序列,任意一条都可以,用print()函数,递归输出即可

3、题目:

Question 1: Is Bigger Smarter?

The Problem

Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the IQ's are decreasing.

The input will consist of data for a bunch of elephants, one elephant per line, terminated by the end-of-file. The data for a particular elephant will consist of a pair of integers: the first representing its size in kilograms and the second representing its IQ in hundredths of IQ points. Both integers are between 1 and 10000. The data will contain information for at most 1000 elephants. Two elephants may have the same weight, the same IQ, or even the same weight and IQ.

Say that the numbers on the i-th data line are W[i] and S[i]. Your program should output a sequence of lines of data; the first line should contain a numbern; the remaining n lines should each contain a single positive integer (each one representing an elephant). If thesen integers are a[1], a[2],..., a[n]then it must be the case that

   W[a[1]] < W[a[2]] < ... < W[a[n]]

and

   S[a[1]] > S[a[2]] > ... > S[a[n]]

In order for the answer to be correct, n should be as large as possible. All inequalities are strict: weights must be strictly increasing, and IQs must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.

Sample Input

6008 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900

Sample Output

44597

 

4、ac代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 1005struct node{    int w;    int v;    int id;}a[N];int dp[N];int prev[N];int maxx;int cmp(node a,node b){    if(a.w==b.w)    return a.v>b.v;    else    return a.w<b.w;}void print(int i){    if(maxx--)    {        print(prev[i]);        printf("%d\n",a[i].id+1);//下标从1开始    }}int main(){    int i=0,temp1,temp2;    while(scanf("%d%d",&temp1,&temp2)!=EOF)    {//        if(temp1==0 && temp2==0)//        break;        a[i].w=temp1;        a[i].v=temp2;        a[i].id=i;        i++;    }    int n=i,pos;    maxx=-1;    sort(a,a+n,cmp);    for(int i=0;i<n;i++)    {        dp[i]=1;        prev[i]=i;    }    for(int i=0;i<n;i++)    {        for(int j=0;j<i;j++)        {            if(a[i].w>a[j].w && a[i].v<a[j].v && dp[j]+1>dp[i])            {                dp[i]=dp[j]+1;                prev[i]=j;//记录i之前的满足的j            }            if(dp[i]>maxx)            {                maxx=dp[i];                pos=i;            }        }    }    printf("%d\n",maxx);    print(pos);    return 0;}


 

4、用LIS没做对的代码,待改正

#include<stdio.h>#include<string.h>#define N 1005#include<algorithm>using namespace std;struct node{    int w;    int v;} a[N];struct node1{    int id;    int value;} stack[N];int cmp(node a,node b){    if(a.v==b.v)        return a.w<b.w;    else        return a.v>b.v;}int dp[N];int prev[N];int n,maxx,maxid;void LIS(){    //memset(stack,0,sizeof(stack));    for(int i=0; i<=N; i++)    {        stack[i].value=0;        stack[i].id=0;    }    memset(dp,0,sizeof(dp));    stack[0].value=-9999999;    stack[0].id=n+1;    a[n+1].v=999999;    int top=0;    maxx=-1;    for(int i=0; i<n; i++)    {        if(a[i].w>stack[top].value && a[i].v<a[stack[top].id].v)        {            printf("**%d\n",a[i].w);            stack[++top].value=a[i].w;            stack[top].id=i;            dp[i]=top;            prev[i]=stack[top-1].id;        }        else        {            int l=0,r=top;            while(l<=r)            {                int mid=(l+r)>>1;                if(stack[mid].value<=a[i].w )                {                    r=mid-1;                }                else                    l=mid+1;            }            printf("&&%d %d\n",a[stack[l].id].w,a[i].w);            if(l-1>=0)            {                if(a[i].v<a[stack[l-1].id].v && a[i].v>a[stack[l+1].id].v)                {                    printf("&%d %d\n",a[stack[l].id].v,a[i].v);                    prev[i]=stack[l-1].id;                    stack[l].value=a[i].w;                    stack[l].id=i;                    dp[i]=l;                }            }            else            {                if( a[i].v>a[stack[l+1].id].v)                {                    printf("&%d %d\n",a[stack[l].id].v,a[i].v);                    prev[i]=stack[l-1].id;                    stack[l].value=a[i].w;                    stack[l].id=i;                    dp[i]=l;                }            }        }        if(dp[i]>=maxx)        {            maxx=dp[i];            maxid=i;        }    }}int main(){    int tmp1,tmp2;    int i=0;    while(scanf("%d%d",&tmp1,&tmp2)!=EOF)    {        if(tmp1==0 && tmp2==0)            break;        a[i].w=tmp1;        a[i].v=tmp2;        i++;    }    n=i;    sort(a,a+n,cmp);    for(int i=0; i<n; i++)    {        printf("%d %d\n",a[i].w,a[i].v);    }    LIS();    printf("****\n");    printf("%d\n",maxx);    i=maxid;    int ans[N],j=0;    while(prev[i]!=n+1)    {        ans[j]=prev[i];        i=ans[j];        j++;    }    for(int i=j-1; i>=0; i--)        printf("%d\n",ans[i]);    return 0;}/*6008 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 19000 0*/


 

 

原创粉丝点击