UVa 10131: Is Bigger Smarter?
动态规划题。类似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?的更多相关文章
- uva 10131 Is Bigger Smarter?(DAG最长路)
题目连接:10131 - Is Bigger Smarter? 题目大意:给出n只大象的属性, 包括重量w, 智商s, 现在要求找到一个连续的序列, 要求每只大象的重量比前一只的大, 智商却要小, 输 ...
- UVA 10131 - Is Bigger Smarter? (动态规划)
Is Bigger Smarter? The Problem Some people think that the bigger an elephant is, the smarter it is. ...
- Uva 10131 Is Bigger Smarter? (LIS,打印路径)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意: ...
- UVA 10131 Is Bigger Smarter?(DP最长上升子序列)
Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elepha ...
- 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 ...
- uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)
题目链接 题意:有好多行,每行两个数字,代表大象的体重和智商,求大象体重越来越大,智商越来越低的最长序列,并输出. 思路:先排一下序,再按照最长上升子序列计算就行. 还有注意输入, 刚开始我是这样输入 ...
- UVA - 10131Is Bigger Smarter?(DAG上的DP)
题目:UVA - 10131Is Bigger Smarter? (DAG) 题目大意:给出一群大象的体重和IQ.要求挑选最多的大象,组成一个序列.严格的体重递增,IQ递减的序列.输出最多的大象数目和 ...
- UVA 10131题解
第一次写动态规划的代码,整了一天,终于AC. 题目: Question 1: Is Bigger Smarter? The Problem Some people think that the big ...
- uva 10131
DP 先对大象体重排序 然后寻找智力的最长升序子列 输出路径.... #include <iostream> #include <cstring> #include &l ...
随机推荐
- socket(套接字)
客户端: 创建套接字(socket) 连接服务器(connect) 通信(send,recv或者write,read) 关闭套接字(closesocket) 示例代码: int main(int ar ...
- SGU 134.Centroid( 树形dp )
一道入门树dp, 求一棵树的重心...我是有多无聊去写这种题...傻X题写了也没啥卵用以后还是少写好.. ----------------------------------------------- ...
- Hive学习之动态分区及HQL
Hive动态分区 1.首先创建一个分区表create table t10(name string) partitioned by(dt string,value string)row format d ...
- Windows Phone
错误: DEP6100 : 引导阶段“正在连接到设备”出现以下意外错误: SmartDeviceException - Windows Phone IP over USB Transport (IpO ...
- HortonWorks
http://zh.hortonworks.com/products/hortonworks-sandbox/
- selenium的config.ini
config.ini # What WebDriver to use for the tests#driver=phantomjs#driver=firefoxdriver=chrome#driver ...
- HDU 5446 Unknown Treasure(Lucas定理+CRT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...
- g711u与g729比較编码格式
•711a-编解码格式为G.711 alaw •g711u-编解码格式为G.711 ulaw (the default) •g729-编解码格式为G.729 •g729a-编解码格式为G.729a 上 ...
- Android Animation学习(一) Property Animation介绍
Android Animation Android framework提供了两种动画系统: property animation (introduced in Android 3.0)和view an ...
- AngularJs 简单入门
1.AngularJs 是什么以及应用程序组成的三部分 AngularJS是一个开发动态Web应用的框架.它让你可以使用HTML作为模板语言并且可以通过扩展的HTML语法来使应用组件更加清晰和简洁.它 ...