动态规划题。类似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. errorPlacement的位置问题

    做一个前端的验证,使用了JQUERY.Validate 在errorPlacement上纠结了半天: 百度大多数都是一个答案: errorPlacement: function(error, elem ...

  2. Qt QToolTip 控件背景的 QSS 设置方法(摘抄)

    Qt/C++/CSS: QTooltip stylesheet background colour Hi there, I've recently come across a problem deve ...

  3. tengine install

    ./configure --prefix=/home/admin/local/tengine --with-http_stub_status_module --with-http_ssl_module ...

  4. mysql批量上传数据

    private object BlubckMysql(List<xiaoyao_blogs_pictureModel> list, string connect) { var sqllis ...

  5. The following classes could not be found: - android.support.v7.internal.widget.ActionBarOverlayLayou解决方案

    如图出现如下的错误的时候,一般都是升级Androdi Studio 后导致的,引入库不全,或者其他 东西缺少,可以如下解决方案: 或者如下的解决方案: 在布局文件的Design界面中,修改原来的App ...

  6. 算法学习笔记(LeetCode OJ)

    ================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com == ...

  7. halcon与C#混合编程进阶版

    这篇主要是C#和Halcon的混合编程,在此基础上对按键不同功能的划分,以及图片适应窗口和从本地打开图片. 新手来这里:http://www.cnblogs.com/badguy518/p/55150 ...

  8. ElaticSearch网站

    http://www.tuicool.com/articles/r2QJVr http://so.searchtech.pro/articles/2013/06/16/1371392427213.ht ...

  9. Android 优化性能之 如何避免--过度绘制

    可能有些人不明白什么是过度绘制,简单言,我们app一个页面所显示的效果是由像素一帧一帧绘制而成.过度绘制就是意味着这一帧被绘制多次.如果是静态的布局,可能影响不是很大,如果是动态的,比如ListVie ...

  10. HDU 5875 Function(ST表+二分)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...