UVA 10131题解
第一次写动态规划的代码,整了一天,终于AC。
题目:
Question 1: Is Bigger Smarter?
The Problem
Some people think that the bigger an elephant is, the smarter it is. To disprove this, you want to take the data on a collection of elephants and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the IQ's are decreasing.
The input will consist of data for a bunch of elephants, one elephant per line, terminated by the end-of-file. The data for a particular elephant will consist of a pair of integers: the first representing its size in kilograms and the second representing its IQ in hundredths of IQ points. Both integers are between 1 and 10000. The data will contain information for at most 1000 elephants. Two elephants may have the same weight, the same IQ, or even the same weight and IQ.
Say that the numbers on the i-th data line are W[i] and S[i]. Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing an elephant). If these n integers are a[1], a[2],..., a[n] then it must be the case that
W[a[1]] < W[a[2]] < ... < W[a[n]]
and
S[a[1]] > S[a[2]] > ... > S[a[n]]
In order for the answer to be correct, n should be as large as possible. All inequalities are strict: weights must be strictly increasing, and IQs must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.
Sample Input
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
Sample Output
4
4
5
9
7
题解:
两种解法:
步骤:1. 对大象按照重量进行递增排序,相同体重的按照IQ递减排序,得到大象序列A
2. 求A进行最长递减子序列,动态规划法。
设f(i)表示L中以ai为末元素的最长递增子序列的长度。则有如下的递推方程:
这个递推方程的意思是,在求以ai为末元素的最长递增子序列时,找到所有序号在L前面且小于ai的元素aj,即j<i且aj<ai。如果这样的元素存在,那么对所有aj,都有一个以aj为末元素的最长递增子序列的长度f(j),把其中最大的f(j)选出来,那么f(i)就等于最大的f(j)加上1,即以ai为末元素的最长递增子序列,等于以使f(j)最大的那个aj为末元素的递增子序列最末再加上ai;如果这样的元素不存在,那么ai自身构成一个长度为1的以ai为末元素的递增子序列。
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int index;
int w;
int iq;
} Niu;
int comp(const void * a, const void * b)
{
int v=((Niu *)a)->w-((Niu *)b)->w;
if( v== 0)
{
return ((Niu *)b)->iq-((Niu *)a)->iq;
}else return v;
} Niu ns[1002];
int f[1002]; //以ns[i]为末尾的序列长度
int mi[1002];
int out[1002];
int main()
{
int c=0,c2,n,m, i,j,t1,t2; while(scanf("%d %d",&t1, &t2)==2){
ns[c].w=t1;
ns[c].iq=t2;
ns[c].index=c+1;
c++;
}
qsort(ns,c,sizeof(Niu),comp); //排序,重量递增排序,如果重量相同,iq递减排序 f[0]=1; //初始化
//dp
for(i=0; i<c; i++) //
{
int max=0;
int midx=-1;
for(j=0; j<i;j++) //找0到i-1见f(i)最长并且
{
if(ns[i].iq<ns[j].iq &&ns[i].w!=ns[j].w && max<f[j]){
max=f[j];
midx=j;
}
}
f[i]=max+1;
mi[i]=midx;
}
int max=-1;
int midx;
for(i=0; i<c; i++){ if(f[i]>max){max=f[i];midx= i;}
}
printf("%d\n",max);
int cxxx=0;
while(1) //输出
{
out[cxxx]= ns[midx].index;
cxxx++;
midx=mi[midx];
if(midx==-1)break;
}
for(i=cxxx; i>0;i--)
{
printf("%d\n", out[i-1]);
} return 0;
}
对于这个问题,还有另一个解法,是将最长递减子序列转变为最长公共子序列问题。
不过很复杂,导致一直WE,我的错误代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int index;
int w;
int iq;
} Niu;
int comp(const void * a, const void * b)
{
int v=((Niu *)a)->w-((Niu *)b)->w;
if( v== 0)
{
return ((Niu *)b)->iq-((Niu *)a)->iq;
}else return v;
}
int comp2(const void * a, const void * b)
{
int v=((Niu *)b)->iq-((Niu *)a)->iq;
if(v==0){
return ((Niu *)a)->w-((Niu *)b)->w;
}else
return v;
}
Niu ns[1001];
Niu ns2[1001];
Niu tmp[1002];
int cm[1002][1002];
int tag[1002][1002]; // 1,-1,-2
int result[1002];
int resultSize;
void dp(int n,int m) //c表示大小
{
int i,j,k;
k=0;
for(i=0; i<n; i++)
{
for(j=0;j<m; ++j)
{
if(ns[i].iq==ns2[j].iq)
{
cm[i+1][j+1]=cm[i][j]+ 1;
tag[i+1][j+1] = 1;
}else if(cm[i][j+1]>=cm[i+1][j]){
cm[i+1][j+1]=cm[i][j+1];
tag[i+1][j+1] = -1;
}else{
cm[i+1][j+1]=cm[i+1][j];
tag[i+1][j+1] = -2;
}
}
}
}
void printLCS(int i, int j)
{
if(i==0 || j==0)
return;
if(tag[i][j]==1){
printLCS(i-1,j-1);
result[resultSize]=ns[i-1].index;
resultSize++;
}else if(tag[i][j]==-1)
{
printLCS(i-1,j);
}else {
printLCS(i,j-1);
}
} /*
test case 1:
1000 900
1000 800
1010 900
1010 800 test case 2:
1000 900
1000 900
1010 900
1010 800 test case 3:
100 20
110 20
120 20 test case 4:
100 20
100 20
100 20
100 20
110 19
110 20
test case 5: the result should by
100 20
110 19
110 20 */
int main()
{
int c=0,c2,n,m, i,j,t1,t2; while(scanf("%d %d",&t1, &t2)==2){
ns[c].w=t1;
ns[c].iq=t2;
ns[c].index=c+1;
c++;
} for(i=0; i<c; i++)
{
ns2[i] = ns[i];
}
qsort(ns,c,sizeof(Niu),comp); printf("ns:\n");
for(i=0; i<c; i++)
{
printf("%d %d\n", ns[i].w,ns[i].iq);
} qsort(ns2,c,sizeof(Niu),comp2);
//要把ns2的重复iq的项去掉
j=0;
tmp[0]=ns2[0];
for(i=1;i<c;i++){
if(ns2[i].iq!=tmp[j].iq){
++j;
tmp[j]=ns2[i];
}
} c2=j+1; //去重复大小
for(i=0; i<c2; i++){
ns2[i]=tmp[i];
}
printf("c2:%d\n",c2); dp(c,c2);
printLCS(c,c2); //去重复
//
/* */
int ss=resultSize;
printf("size:%d\n",resultSize);
for(i=0; i<resultSize; i++){
printf("%d\n",result[i]);
}
int pre=0;
for(i=1; i<resultSize; i++){
if(ns[result[i]-1].w!=ns[result[pre]-1].w && ns[result[i]-1].iq!=ns[result[pre]-1].iq ){
pre=i;
}else{
ss--;
result[i]=-1;
}
}
printf("ss:%d\n",ss);
for(i=0; i<resultSize; i++){
if(result[i]!=-1)printf("%d\n", result[i]);
}
return 0;
}
UVA 10131题解的更多相关文章
- Uva 10131 Is Bigger Smarter? (LIS,打印路径)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意: ...
- uva 10131 Is Bigger Smarter?(DAG最长路)
题目连接:10131 - Is Bigger Smarter? 题目大意:给出n只大象的属性, 包括重量w, 智商s, 现在要求找到一个连续的序列, 要求每只大象的重量比前一只的大, 智商却要小, 输 ...
- 位运算基础(Uva 1590,Uva 509题解)
逻辑运算 规则 符号 与 只有1 and 1 = 1,其他均为0 & 或 只有0 or 0 = 0,其他均为1 | 非 也就是取反 ~ 异或 相异为1相同为0 ^ 同或 相同为1相异为0,c中 ...
- 【OI】计算分子量 Molar mass UVa 1586 题解
题目:(由于UVa注册不了,还是用vjudge) https://vjudge.net/problem/UVA-1586 详细说明放在了注释里面.原创. 破题点在于对于一个元素的组合(元素+个数),只 ...
- uva 10131
DP 先对大象体重排序 然后寻找智力的最长升序子列 输出路径.... #include <iostream> #include <cstring> #include &l ...
- uva 10131 Is Bigger Smarter ? (简单dp 最长上升子序列变形 路径输出)
题目链接 题意:有好多行,每行两个数字,代表大象的体重和智商,求大象体重越来越大,智商越来越低的最长序列,并输出. 思路:先排一下序,再按照最长上升子序列计算就行. 还有注意输入, 刚开始我是这样输入 ...
- 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?(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?
动态规划题.类似UVa103 Stacking Box,都是题目给一种判断嵌套的方法然后求最长序列.提前对数据排序可以节省一些时间开销. 我的解题代码如下: #include <iostream ...
随机推荐
- 解魔方的机器人攻略15 – 安装 Eclipse
由 动力老男孩 发表于 2009/12/27 17:40:49 在远古时代,程序员们通常用写字板来编写Java程序,然后用Javac.exe和Java.exe来编译和执行.对于NXT来说,对应的命令是 ...
- openfire源码修改后如何打包部署到linux服务器上
原文:http://blog.csdn.net/jinzhencs/article/details/50457152 1.linux版本的3.10.3解压部署启动(过程略,参考我的另一篇博文http: ...
- go+mysql实现页面的增删改查练习
原文地址:http://www.niu12.com/article/35 初次学go,在了解一些基础之后就开始做一个用户的增删改查来回顾知识,有很多数据验证和安全漏洞并没有考虑,只当作联系 前提:下载 ...
- 使用PHPEXCEL导入数据到数据库
导出功能参考:http://www.cnblogs.com/zhouqi666/p/5978017.html 比较严重的问题:当遇到excel数据量比较大的时候,会发生内存溢出的情况,目前无法解决 e ...
- Ubuntu下安装配置JDK
第一步:下载jdk-7-linux-i586.tar.gz wget -c http://download.oracle.com/otn-pub/java/jdk/7/jdk-7-linux-i586 ...
- 数据库读写锁的实现(C++)
一.基本概念 在数据库中,对某数据的两个基本操作为写和读.分布有两种锁控制:排它锁(X锁).共享锁(S锁). 排它锁(x锁):若事务T对数据D加X锁,则其他不论什么事务都不能再对D加不论什么类型的锁. ...
- httpd配置文件中重写函数Rewrite
[RewriteCond%{HTTP_HOST}^(www\.)?xxx\.com$] 这是重写条件,前面%{HTTP_HOST}表示当前访问的网址,只是指前缀部分,格式是www.xxx.com不包括 ...
- python gzip压缩
1 import urllib2 2 from StringIO import StringIO 3 import gzip 4 5 def loadData(url): 6 request = ur ...
- solrCloud分布式检索流程
FROM: http://blog.csdn.net/duck_genuine/article/details/17014991 好久没写solr的文章了,刚好需要在公司作个分享,先总结一些先. 引用 ...
- 全栈技术经理——团队管理:每周问问你的团队这这些问题 V1.0
全栈技术经理--团队管理:每周问问你的团队这这些问题 V1.0 1.本周取得了哪些进展? 通过回答这个问题可以让员工庆祝甚至夸耀一些自己的成果,包括那些跟最高优先级不相干而被忽视的小事情.借此你也 ...