Is Bigger Smarter?+uva+简单dp(最长公共升降子序列的变形)
来源:互联网 发布:ee域名注册 编辑:程序博客网 时间:2024/05/16 15:39
Description
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 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900
Sample Output
44597
解决方案:直接按照最长公共升降子序列的思想。dp[i]=max{dp[j]+1|a[i]>a[j]},最长公共生子序列
code:#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<stack>using namespace std;int dp[1003];const int maxn=1003;struct node{ int p; int W,S;} N[maxn];bool cmp(node a,node b){ if(a.W!=b.W)return a.W<b.W; else return a.S>b.S;}int fa[1003];int main(){ memset(dp,0,sizeof(dp)); memset(fa,-1,sizeof(fa)); int i=1; while(~scanf("%d%d",&N[i].W,&N[i].S)) { N[i].p=i; i++; } sort(N+1,N+1+i,cmp); int st=1,Max=0; for(int j=1; j<=i; j++) { dp[j]=1; for(int k=1; k<j; k++) { if(N[j].W>N[k].W&&N[j].S<N[k].S) { if(dp[j]<dp[k]+1) { dp[j]=dp[k]+1; fa[j]=k; } } } if(dp[j]>Max){ Max=dp[j]; st=j; } } printf("%d\n",Max); stack<int>S; S.push(st); while(fa[st]!=-1){ S.push(fa[st]); st=fa[st]; } while(!S.empty()){ int s=S.top(); printf("%d\n",N[s].p); S.pop(); } return 0;}
- Is Bigger Smarter?+uva+简单dp(最长公共升降子序列的变形)
- UVA 10131 Is Bigger Smarter? (DP,最长条件子序列)
- Uva 10131 Is Bigger Smarter 最长递减子序列
- UVA - 10131 Is Bigger Smarter? 最长上升子序列
- UVA 10131 - Is Bigger Smarter?非连续的单调递增的最长子序列的长度
- uva10131 Is Bigger Smarter?(经典DP,最长上升子序列,注意保存路径部分)
- UVA - 10131 Is Bigger Smarter?(dp+最大升序子序列)
- UVA 10131 Is Bigger Smarter? (DP)
- Uva 10131-Is Bigger Smarter?(DP)
- UVA - 10131Is Bigger Smarter?(DAG上的DP)
- uvaoj 10131 Is Bigger Smarter? 最长上升子序列(LIS)
- UVA 10131Is Bigger Smarter? 【严格单调递增子序列】
- uva 10131 Is Bigger Smarter?(DAG最长路)
- Vacation+uva+简单dp(最长公共升子序列)
- UVA 10131 Is Bigger Smarter? DP
- UVa 10131 Is Bigger Smarter? (DP&LIS)
- UVA 10131 Is Bigger Smarter ? DP ,Commencel
- Is Bigger Smarter? - UVa 10131 dp
- php pthreads 列子:1
- Linux服务器Jboss运行环境搭建步骤和开机自动启动脚本编写运行
- git使用--提交代码
- maven 更新webapp version 和 jdk version
- iOS网络编程-ASIHTTPRequest异步请求
- Is Bigger Smarter?+uva+简单dp(最长公共升降子序列的变形)
- 设计模式六大原则(4):接口隔离原则
- Windows消息大全
- 监听手机状态之PhoneStateListener
- Gson解析json
- NSString 的stringByTrimmingCharactersInSet使用
- 三.CameraModule
- 基于SMTP的JAVA邮件发送程序!
- UniCode 下 CString 转 char* 的方法