uva--10131Is Bigger Smarter? +dp

题意:

有人说大象越重就越聪明,为了推翻的它的结论,给你一组大象的体重和智商的数组,你需要找出一组最长的随着体重增加智商下降的序列。

思路:

按照体重排一下序,然后就变成求一个智商最长下降子序列的问题了。

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

typedef struct
{
    int w,s;
    int id;
}P;
P p[1100];

int cmp(P p1,P p2)
{
    return p1.w<p2.w;
}

int fa[1100],max1=-1;

void print(int i)
{
    if(max1--)
    {
        print(fa[i]);
        printf("%d\n",p[i].id);
    }
}

int main()
{
    int i,j,x,y,n=1;
    while(scanf("%d%d",&x,&y)!=EOF)
    {
         p[n].w=x; p[n].s=y;
         p[n].id=n;
         n++;
    }
    sort(p+1,p+n,cmp);
    int dp[1100];
    memset(fa,-1,sizeof(fa));
    for(i=1;i<n;i++)
        dp[i]=1;
    for(i=1;i<n;i++)
      for(j=1;j<i;j++)
      {
          if(p[i].s<p[j].s&&dp[i]<dp[j]+1)
          {
              dp[i]=dp[j]+1;
              fa[i]=j;
          }
      }
    int t;
    for(i=1;i<n;i++)
       if(dp[i]>max1)
          max1=dp[i],t=i;
    printf("%d\n",max1);
    print(t);
 return 0;
}
时间: 2024-07-31 14:35:25

uva--10131Is Bigger Smarter? +dp的相关文章

UVA - 10131Is Bigger Smarter?(DAG上的DP)

题目:UVA - 10131Is Bigger Smarter? (DAG) 题目大意:给出一群大象的体重和IQ.要求挑选最多的大象,组成一个序列.严格的体重递增,IQ递减的序列.输出最多的大象数目和这些大象的序列(当中一种就能够). 解题思路:DAG上的DP.和之前的一篇相似.uva437 - The Tower of Babylon(DAG上的DP).就是将每两仅仅大象满足上面的序列要求的形成一条有向边. 之后就是DAG上的DP.然后再路径输出. 代码: #include <cstdio>

Uva 10131-Is Bigger Smarter?(DP)

题目链接:点击打开链接 DAG(有向无环图)上的最长路+打印路径 建图很简单,对于两点 a b, 能够由a到b的条件是w[a]<w[b]&&s[a]>s[b] 注意是有向图. 设dp[i] 为以i为起点的最长路的长度,dp[i]= max(dp[i],dp[j]+1)  枚举j (j是和i相连的点) #include <algorithm> #include <iostream> #include <cstring> #include <

UVA 10131 Is Bigger Smarter?(DP)

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 t

UVA 10131 Is Bigger Smarter?(DP最长上升子序列)

Description 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 in

UVA 10131 Is Bigger Smarter? 【严格单调递增子序列】

题目:UVA 10131 Is Bigger Smarter 题意:给出大象的身高和体重,求身高递增且体重递减的最长序列,都是严格的,并打印序列. 分析:就是先对身高按自增排序,然后求一个单调递减子序列,严格单调的,所以加一句判断,然后打印序列,用一个数组保存就好了 开始想的是先预处理掉重复的,提交wa了,这样不行,因为你不知道体重是最高的还是最低的,可能开始留高的好,后面低的比较好.所以..... AC代码: #include<iostream> #include<cstdio>

uva 10131 Is Bigger Smarter? (DAG)

uva 10131 Is Bigger Smarter? 题目大意:当一只大象的体重大于另一只的体重,且智商小于另一只的智商,该大象便可以"嵌套"另一只大象.问,最长的嵌套方式.(答案不唯一) 解题思路:DAG. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; struct ELE{ int w

UVA 12105 - Bigger is Better(DP+高精度)

题目链接:12105 - Bigger is Better 题意:一些火柴,问你能组成整除m最大的数字是多少. 思路:dp[i][j]表示用i根火柴,组成%m余数为j的最大数字,末尾多一个数字k的状态就是dp[i + num[k]][(j * 10 + k) % m],由于最多可能50位数,所以要用高精度. 注意一个优化点,由于高精度的计算上只需要乘10+k,常规的高精度乘法复杂度还是有点高会超时,所以用数组去模拟,每次*10 + k的时候就往后多一位即可. 代码: #include <stdi

uva10131 Is Bigger Smarter?(经典DP,最长上升子序列,注意保存路径部分)

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 th

UVa 10131 Is Bigger Smarter? (LDS+数据结构排序)

Is Bigger Smarter? Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. To disprove this, y