动态规划题。类似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. BZOJ 1832: [AHOI2008]聚会( LCA )

    LCA模板题...不难发现一定是在某2个人的LCA处集合是最优的, 然后就3个LCA取个最小值就OK了. 距离就用深度去减一减就可以了. 时间复杂度O(N+MlogN) (树链剖分) -------- ...

  2. 查看memcached依赖的库

    LD_DEBUG=libs ./memcached -v

  3. hive支持sql大全

    转自:http://www.aboutyun.com/thread-7316-1-1.html 一.关系运算:1. 等值比较: = 语法:A=B 操作类型:所有基本类型 描述: 如果表达式A与表达式B ...

  4. R与数据分析旧笔记(十六) 基于密度的方法:DBSCAN

    基于密度的方法:DBSCAN 基于密度的方法:DBSCAN DBSCAN=Density-Based Spatial Clustering of Applications with Noise 本算法 ...

  5. CSS3 旋转 太阳系

    参考https://www.tadywalsh.com/web/cascading-solar-system/ 首先 旋转有两种方式  一种是使用 transform-origin  另一种是tran ...

  6. Android TextWatcher应用实例

    (1)使用TextWathcer限制输入字符个数布局中EditText在android布局中经常用到,对EditText中输入的内容也经常需要进行限制,我们可以通过TextWatcher去观察输入框中 ...

  7. python总结

    环境:django,numpy,matplotlib, 解释语言:开发效率高,通用性强,内置方便的数据容器,易于扩展和嵌入. 语言:lua--嵌入式/网络/APP,erlang--嵌入式,python ...

  8. rdo(remote data objects) repo openstack icehouse

    problem making ssl connection Error: Cannot retrieve repository metadata (repomd.xml) for repository ...

  9. 杭州电子科技大学Online Judge 之 “确定比赛名次(ID1285)”解题报告

    杭州电子科技大学Online Judge 之 "确定比赛名次(ID1285)"解题报告 巧若拙(欢迎转载,但请注明出处:http://blog.csdn.net/qiaoruozh ...

  10. POJ 3623 Best Cow Line, Gold(字符串处理)

    题意:给你一个字符串,让你重新排列,只能从头或者尾部取出一个放到新字符串队列的最后.按照字典序. 解决方法:比较前后两个的大小,谁小输出谁,相等,就往当中比来确定当前应该拿最前面的还是最后面的,如果再 ...