动态规划题。类似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. socket中的option

    /// Set an option on the socket. /** * This function is used to set an option on the socket. * * @pa ...

  2. 彻底解决 LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    最近我的VS2010不知道怎么回事,平时用的好好的,近期竟然出现了所谓的 LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 头痛万分,查了各种资料一 ...

  3. XSS【跨站脚本攻击】

    从客户端(txt="<script><a href="www...")中检测到有潜在危险的 Request.Form 值. 如果你使用的是.NET 3. ...

  4. Canvas 雾玻璃

    Demo http://lumixraku.github.io/demos/Fog/Fog.html Canvas tutorial 给大家安利一个学习Canvas的站点 http://www.htm ...

  5. .NET DataGrid 导出Excel 无分页

    #region 导出Excel // protected void BtnExcelClick(object sender, EventArgs e) { ToExcel(); } public vo ...

  6. Python GUI开发环境的搭建

    原文:Python GUI开发环境的搭建 最近对Python的开发又来了兴趣,对于Python的开发一直停留在一个表面层的认识,玩的部分比较大. Python的入手简单,语法让人爱不释手,在网络通信方 ...

  7. nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException

    You should autowire interface AbstractManager instead of class MailManager. If you have different im ...

  8. ubuntu-12.04.4-server安装

    一.系统ISO下载      下载地址:http://www.ubuntu.com/download       根据自己的需求下载,我的电脑配置一般,因此选择32位的.   二.虚拟机配置      ...

  9. 第八届河南省赛G.Interference Signal(dp)

    G.Interference Signal Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 35  Solved: 17 [Submit][Status ...

  10. hdu2554-N对数的排列问题

    http://acm.hdu.edu.cn/showproblem.php?pid=2554 假设所有的2n个数据的位置分别从1~2n标号. 现在假设其中第ai个数据(双胞胎),和bi.那么他们的位置 ...