uva10131 Is Bigger Smarter?(经典DP,最长上升子序列,注意保存路径部分)
来源:互联网 发布:纬创软件 上海 编辑:程序博客网 时间:2024/06/05 11:54
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
/*Author:ZCC;Solve:对第二个关键字降序,就转换为找第一个关键字的LIS*/#include<iostream>#include<algorithm>#include<map>#include<cstdio>#include<cstdlib>#include<vector>#include<cmath>#include<cstring>#include<stack>#include<string>#include<fstream>#define pb(s) push_back(s)#define cl(a,b) memset(a,b,sizeof(a))#define bug printf("===\n");using namespace std;typedef vector<int> VI;#define rep(a,b) for(int i=a;i<b;i++)#define rep_(a,b) for(int i=a;i<=b;i++)#define P pair<int,int>#define bug printf("===\n");#define PL(x) push_back(x)#define X first#define Y second#define vi vector<int>#define rep(i,x,n) for(int i=x;i<n;i++)#define rep_(i,x,n) for(int i=x;i<=n;i++)const int maxn=2100;const int inf=999999;typedef long long LL;struct node{ int x,y; int id; bool operator<(const node&t) const{ return y>t.y; }}p[maxn];int f[maxn];int d[maxn];int LIS(int n,int& pos){ int ans=0; for(int i=0;i<n;i++){ d[i]=1; for(int j=0;j<i;j++)if(p[i].x>p[j].x&&p[i].y<p[j].y&&d[i]<d[j]+1){ f[i]=j; d[i]=d[j]+1; } if(ans<d[i]){//这个地方,更新,这里是不能写在上面的if里,因为即使的d[i]不是路径的最后一个,最终结果也会更新到他 ans=d[i]; pos=i; } } return ans;}void out(int x){ if(x==-1)return ; out(f[x]); printf("%d\n",p[x].id);}int main(){ int i=0; while(~scanf("%d%d",&p[i].x,&p[i].y)){ p[i].id=i+1; i++; } cl(d,0); cl(f,-1); int n=i; sort(p,p+n);// for(int i=0;i<n;i++){// printf("%d %d %d\n",p[i].x,p[i].y,p[i].id);// } printf("%d\n",LIS(n,i)); out(i); return 0;}/*6008 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900*/
- uva10131 Is Bigger Smarter?(经典DP,最长上升子序列,注意保存路径部分)
- UVA - 10131 Is Bigger Smarter? 最长上升子序列
- uvaoj 10131 Is Bigger Smarter? 最长上升子序列(LIS)
- uva10131 Is Bigger Smarter?
- uva10131 - Is Bigger Smarter?
- UVA 10131 Is Bigger Smarter? (DP,最长条件子序列)
- Is Bigger Smarter?+uva+简单dp(最长公共升降子序列的变形)
- Uva 10131 Is Bigger Smarter 最长递减子序列
- UVA10131 Is Bigger Smarter? 简单动态规划
- [动态规划]UVA10131 - Is Bigger Smarter?
- UVA - 10131 Is Bigger Smarter?(dp+最大升序子序列)
- 10131 Is Bigger Smarter?(最长上升序列问题 + 记忆化搜索)
- UVA 10131 - Is Bigger Smarter?非连续的单调递增的最长子序列的长度
- UVA 10131 Is Bigger Smarter? (DP)
- Uva 10131-Is Bigger Smarter?(DP)
- UVA 10131Is Bigger Smarter? 【严格单调递增子序列】
- 最长上升子序列 (dp)
- 最长公共上升子序列 (dp)
- GRE写作必备句型
- Centos7-64bit尝试编译最新版的nginx1.9.3+php7+mysql5.6.25
- 对象析构谈—— delete this 的使用及注意事项
- Laravel常见的问题
- js自定义右键菜单
- uva10131 Is Bigger Smarter?(经典DP,最长上升子序列,注意保存路径部分)
- SourceTree图谱 颜色区分哪个分支
- shell——压缩&解压缩
- TAKEOWN 和 ICACLS 的用法
- ios MVC
- 安装Alcatraz插件管理
- 对象名称要正确
- Mathematica 处理列表中的元素
- HDU oj2007 平方和与立方和