uva 10131 越大越聪明
来源:互联网 发布:手机网络被屏蔽怎么办 编辑:程序博客网 时间:2024/05/20 18:03
1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072
2、题目大意:
给您定很多象的重量和智商IQ值,现在要找出重量从小到大排序,智商IQ值从大小顺序的最长序列
首先将象按照体重从小到大排序,然后求IQ的递减序列,开始用的LIS求的,不对,后来看网上有一种很简单的代码:
定义状态dp[i]表示以当前大象为终点形成的最长序列长度
状态dp[j]表示i之前的符合要求的大象为终点的最长的序列长度
那么有状态转移方程,if(dp[i]<dp[j]+1) ,那么dp[i]=dp[j]+1;
题目要求输出序列,任意一条都可以,用print()函数,递归输出即可
3、题目:
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 numbern; the remaining n lines should each contain a single positive integer (each one representing an elephant). If thesen 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 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900
Sample Output
44597
4、ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 1005struct node{ int w; int v; int id;}a[N];int dp[N];int prev[N];int maxx;int cmp(node a,node b){ if(a.w==b.w) return a.v>b.v; else return a.w<b.w;}void print(int i){ if(maxx--) { print(prev[i]); printf("%d\n",a[i].id+1);//下标从1开始 }}int main(){ int i=0,temp1,temp2; while(scanf("%d%d",&temp1,&temp2)!=EOF) {// if(temp1==0 && temp2==0)// break; a[i].w=temp1; a[i].v=temp2; a[i].id=i; i++; } int n=i,pos; maxx=-1; sort(a,a+n,cmp); for(int i=0;i<n;i++) { dp[i]=1; prev[i]=i; } for(int i=0;i<n;i++) { for(int j=0;j<i;j++) { if(a[i].w>a[j].w && a[i].v<a[j].v && dp[j]+1>dp[i]) { dp[i]=dp[j]+1; prev[i]=j;//记录i之前的满足的j } if(dp[i]>maxx) { maxx=dp[i]; pos=i; } } } printf("%d\n",maxx); print(pos); return 0;}
4、用LIS没做对的代码,待改正
#include<stdio.h>#include<string.h>#define N 1005#include<algorithm>using namespace std;struct node{ int w; int v;} a[N];struct node1{ int id; int value;} stack[N];int cmp(node a,node b){ if(a.v==b.v) return a.w<b.w; else return a.v>b.v;}int dp[N];int prev[N];int n,maxx,maxid;void LIS(){ //memset(stack,0,sizeof(stack)); for(int i=0; i<=N; i++) { stack[i].value=0; stack[i].id=0; } memset(dp,0,sizeof(dp)); stack[0].value=-9999999; stack[0].id=n+1; a[n+1].v=999999; int top=0; maxx=-1; for(int i=0; i<n; i++) { if(a[i].w>stack[top].value && a[i].v<a[stack[top].id].v) { printf("**%d\n",a[i].w); stack[++top].value=a[i].w; stack[top].id=i; dp[i]=top; prev[i]=stack[top-1].id; } else { int l=0,r=top; while(l<=r) { int mid=(l+r)>>1; if(stack[mid].value<=a[i].w ) { r=mid-1; } else l=mid+1; } printf("&&%d %d\n",a[stack[l].id].w,a[i].w); if(l-1>=0) { if(a[i].v<a[stack[l-1].id].v && a[i].v>a[stack[l+1].id].v) { printf("&%d %d\n",a[stack[l].id].v,a[i].v); prev[i]=stack[l-1].id; stack[l].value=a[i].w; stack[l].id=i; dp[i]=l; } } else { if( a[i].v>a[stack[l+1].id].v) { printf("&%d %d\n",a[stack[l].id].v,a[i].v); prev[i]=stack[l-1].id; stack[l].value=a[i].w; stack[l].id=i; dp[i]=l; } } } if(dp[i]>=maxx) { maxx=dp[i]; maxid=i; } }}int main(){ int tmp1,tmp2; int i=0; while(scanf("%d%d",&tmp1,&tmp2)!=EOF) { if(tmp1==0 && tmp2==0) break; a[i].w=tmp1; a[i].v=tmp2; i++; } n=i; sort(a,a+n,cmp); for(int i=0; i<n; i++) { printf("%d %d\n",a[i].w,a[i].v); } LIS(); printf("****\n"); printf("%d\n",maxx); i=maxid; int ans[N],j=0; while(prev[i]!=n+1) { ans[j]=prev[i]; i=ans[j]; j++; } for(int i=j-1; i>=0; i--) printf("%d\n",ans[i]); return 0;}/*6008 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 19000 0*/
- uva 10131 越大越聪明
- UVa Problem 10131 Is Bigger Smarter? (越大越聪明?)
- UVA 10131 Is Bigger Smarter? 越大越聪明? dp,LDS
- 10131越大越聪明
- 一中OJ #1457 越大越聪明[Uva 10131 -> Is Bigger Smarter?] | 动态规划 字典序LIS | 解题报告
- 越大越聪明
- 聪明
- 聪明
- 越大越聪明(输出路径问题)
- 【解题报告】uva10131_Is Bigger Smarter?(越大越聪明, dp, LIS)
- 何谓聪明
- 聪明女人
- 聪明绝顶
- 聪明者三戒
- 聪明无用
- more 聪明
- 聪明与不聪明(转)
- UVa 10131
- TOJ 1037 POJ 2771 LA 3415 Guardian of Decency / 二分图
- 解决utf-8编码下 echo 弹出javascript alert中文乱码解决方法
- c# 判断字符串中是否有中文(是否为纯Asiic)
- 详解IOS开发应用之并发Dispatch Queues
- Flex file html not found 解决方案
- uva 10131 越大越聪明
- memcheck
- XML和JSON两种数据交换格式的比较
- android源码编译常见错误及解决办法1
- 中国物联网 校企联盟站
- spring 配置详解
- 带参数的多线程
- 换用gcc与g++的较低版本
- HDU 4372 - Count the Buildings(组合计数)