动态规划题。类似UVa103 Stacking Box,都是题目给一种判断嵌套的方法然后求最长序列。提前对数据排序可以节省一些时间开销。

我的解题代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; #define MAXN 1005
int N;
int w[MAXN],s[MAXN];
int Rank[MAXN],Index[MAXN],NextInSeq[MAXN],LongLen[MAXN];
int cmp(const void *a, const void *b)
{
int ai = *(int *)a, bi = *(int *)b;
if(w[ai]!=w[bi]) return w[ai]-w[bi];
return s[bi]-s[ai];
};
int dp(int n)
{
if(LongLen[n]) return LongLen[n];
int k = Index[n];
for(int i=k+1; i<N; i++) if(w[n]<w[Rank[i]] && s[n]>s[Rank[i]]) if(LongLen[n]<dp(Rank[i]))
{
LongLen[n] = dp(Rank[i]);
NextInSeq[n] = Rank[i];
}
return ++LongLen[n];
}
int main()
{
N=0;
while(scanf("%d %d",&w[N],&s[N])==2) N++;
for(int i=0; i<N; i++) Rank[i] = i;
qsort(Rank,N,sizeof(int),cmp);
for(int i=0; i<N; i++) Index[Rank[i]] = i;
memset(LongLen,0,sizeof(LongLen)); int anslen = 0, ansi;
for(int i=0; i<N; i++) if(anslen<dp(i)) { anslen = dp(i); ansi = i; }
printf("%d\n%d\n",anslen,ansi+1);
for(int i=1; i<anslen; i++) { ansi = NextInSeq[ansi]; printf("%d\n",ansi+1); }
return 0;
}

UVa 10131: Is Bigger Smarter?的更多相关文章

  1. uva 10131 Is Bigger Smarter?(DAG最长路)

    题目连接:10131 - Is Bigger Smarter? 题目大意:给出n只大象的属性, 包括重量w, 智商s, 现在要求找到一个连续的序列, 要求每只大象的重量比前一只的大, 智商却要小, 输 ...

  2. UVA 10131 - Is Bigger Smarter? (动态规划)

    Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. ...

  3. Uva 10131 Is Bigger Smarter? (LIS,打印路径)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意: ...

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

    Description   Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elepha ...

  5. 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 t ...

  6. uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)

    题目链接 题意:有好多行,每行两个数字,代表大象的体重和智商,求大象体重越来越大,智商越来越低的最长序列,并输出. 思路:先排一下序,再按照最长上升子序列计算就行. 还有注意输入, 刚开始我是这样输入 ...

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

    题目:UVA - 10131Is Bigger Smarter? (DAG) 题目大意:给出一群大象的体重和IQ.要求挑选最多的大象,组成一个序列.严格的体重递增,IQ递减的序列.输出最多的大象数目和 ...

  8. UVA 10131题解

    第一次写动态规划的代码,整了一天,终于AC. 题目: Question 1: Is Bigger Smarter? The Problem Some people think that the big ...

  9. uva 10131

    DP 先对大象体重排序   然后寻找智力的最长升序子列  输出路径.... #include <iostream> #include <cstring> #include &l ...

随机推荐

  1. C++_知识点_指针类型转换

    #include <iostream> using namespace std; int main(){ ] = {, , , , , , , , , }; int* p = (int*) ...

  2. R与数据分析旧笔记(一)基本数学函数的使用

    创建向量矩阵 > x1=c(2,3,6,8) > x2=c(1,2,3,4) > a1=(1:100) > length(a1) [1] 100 > length(x1) ...

  3. ThinkPHP第二十五天(自动完成、用户名密码PHP正则、移位或加密函数)

    1.ThinkPHP自动完成功能 跟昨天的自动验证功能类似,也是需要在自定义的UserModel类,进行使用. 使用方法:定义$_auto属性 $_auto = array( array(完成字段,完 ...

  4. codevs 1256 打鼹鼠 LIS

    题目链接 题目描述 Description 鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的. 根据这个特点阿Q编写了一个打鼹鼠的游戏:在一个n*n的网格中,在某些时 ...

  5. Windows Phone 8.1 发送http 网络请求。

    在windows phone 8.1 中可以用 HttpClient 类来发送http 请求. 例子: try { Uri uri = new Uri(@"http://api.map.ba ...

  6. 转: 关于异步promises

    迄今为止,可能每个JavaScript开发者和他们的祖母都听说过Promises.如果你没有,那么你即将会.promises的概念是由CommonJS小组的成员在 Promises/A规范 中提出来的 ...

  7. WinAPI: GetVolumeInformation - 读取文件系统信息

    //声明: GetVolumeInformation(   lpRootPathName: PChar;              {磁盘驱动器代码字符串}   lpVolumeNameBuffer: ...

  8. HDU 4436 str2int(后缀自动机)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4436 [题目大意] 给出一些字符串,由0~9组成,求出所有不同子串的和. [题解] 将所有字符串添 ...

  9. 张王李相亲应用if else

    package hello; public class to { public static void main(String[]args){ int a =1,b=0; int c =1,d=0; ...

  10. Java图形化界面设计——中间容器(Jpanel)

    1.  将组件添加到JFrame中 方式之一: frame.getContentPane().add(childComponent) 用getContentPane()方法获得JFrame的内容面板, ...