hdu 1160 dp (二维最长上升子序列 记录路径
来源:互联网 发布:淘宝话费流量券大礼包 编辑:程序博客网 时间:2024/06/05 14:26
传送门
FatMouse's Speed
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17219 Accepted Submission(s): 7631
Special Judge
Problem Description
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.
Input
Input contains data for a bunch of mice, one mouse per line, terminated by end of file.
The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.
Two mice may have the same weight, the same speed, or even the same weight and speed.
The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.
Two mice may have the same weight, the same speed, or even the same weight and speed.
Output
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 a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that
W[m[1]] < W[m[2]] < ... < W[m[n]]
and
S[m[1]] > S[m[2]] > ... > S[m[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 speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.
W[m[1]] < W[m[2]] < ... < W[m[n]]
and
S[m[1]] > S[m[2]] > ... > S[m[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 speeds 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
Source
Zhejiang University Training Contest 2001
Recommend
Ignatius | We have carefully selected several similar problems for you: 1024 1159 1025 1081 1080
由于dp是利用之前计算的结果进行递推得到的,因此,每一步的计算都要用到上一步的结。最长上升子序列,就是枚举当前序列的最后一位,然后从前面递推找最优解, 所以这里的pre[i] = j;意思就是当前这个最优解i是由上一个最优解j递推出来的。所以可以用这种方法求的最优路径。
//china no.1#pragma comment(linker, "/STACK:1024000000,1024000000")#include <vector>#include <iostream>#include <string>#include <map>#include <stack>#include <cstring>#include <queue>#include <list>#include <stdio.h>#include <set>#include <algorithm>#include <cstdlib>#include <cmath>#include <iomanip>#include <cctype>#include <sstream>#include <functional>#include <stdlib.h>#include <time.h>#include <bitset>using namespace std;#define pi acos(-1)#define PI acos(-1)#define endl '\n'#define srand() srand(time(0));#define me(x,y) memset(x,y,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0); cin.tie(0);#define FOR(x,n,i) for(int i=x;i<=n;i++)#define FOr(x,n,i) for(int i=x;i<n;i++)#define W while#define sgn(x) ((x) < 0 ? -1 : (x) > 0)#define bug printf("***********\n");#define db double#define ll long longtypedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,1,-1,-1,1};const int dy[]={0,1,0,-1,-1,1,-1,1};const int maxn=2e3+10;const int maxx=1e6+100;const double EPS=1e-8;const double eps=1e-8;const int mod=1e9+7;template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}template <class T>inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}if(IsN) num=-num;return true;}void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}void print(LL a){ Out(a),puts("");}//freopen( "in.txt" , "r" , stdin );//freopen( "data.txt" , "w" , stdout );//cerr << "run time is " << clock() << endl;struct node{ int w,s,id;}Q[maxn];struct Node{ int pre,now,cost;}dp[maxn];bool cmp(node a,node b){ if(a.w!=b.w) return a.w<b.w; return a.s>b.s;}int cnt=0,ans[1005],pre[maxn];int main(){ //freopen( "in.txt" , "r" , stdin ); W(scanf("%d%d",&Q[cnt].w,&Q[cnt].s)!=EOF) {Q[cnt].id=cnt+1,cnt++;} sort(Q,Q+cnt,cmp); int res=0,idex=0; FOr(0,cnt,i) { dp[i].cost=1; FOr(0,i,j) { if(Q[j].w<Q[i].w&&Q[j].s>Q[i].s) { if(dp[i].cost<dp[j].cost+1) { pre[i]=j; dp[i].cost=dp[j].cost+1; } } } if(res<dp[i].cost) { res=dp[i].cost; idex=i; } } print(res); int kase=0; W(idex!=0) { ans[kase++] = idex; idex = pre[idex]; } W(kase>0) { kase--; printf("%d\n",Q[ans[kase]].id); }}
阅读全文
0 0
- hdu 1160 dp (二维最长上升子序列 记录路径
- HDU 1160 FatMouse's Speed 最长上升子序列及记录路径DP
- hdu 1160 最长上升子序列 + 记录路径
- HDU 1160(记录路径的最长上升子序列)
- HDU 1160 FatMouse's Speed (最长上升子序列+记录路径)
- hdu 1160 FatMouse's Speed(最长上升子序列 +记录路径)
- HDU 1160 FatMouse's Speed (最长上升子序列+记录路径)
- hdu 1160 FatMouse's Speed (最长上升子序列 + 记录路径)
- HDU NO.1160 FatMouse's Speed(“最长上升子序列”,记录路径)
- 【最长上升子序列 && 输出路径】HDU
- NUC1776 Tiling Up Blocks【二维最长上升子序列+DP】
- hdu1160 FatMouse's Speed 最长上升子序列以及记录路径 DP
- 最长上升子序列 dp
- DP--最长上升子序列
- HDU 1160(类似于最长上升子序列)
- hdu 1160 排序 + 最长上升子序列
- hdu 1300 Pearls (DP 类似最长上升子序列)
- hdu 1069 Monkey and Banana(dp 最长上升子序列)
- 第二十三篇:JAVA注解
- K:顺序表和链表的比较
- 欢迎使用CSDN-markdown编辑器
- HTML初学笔记5
- iOS 直播 —— 推流
- hdu 1160 dp (二维最长上升子序列 记录路径
- python爬虫备忘(2)
- SQL Tuning 基础概述03
- QML与C++交换数据
- FastDFS文件集群服务器搭建
- python 在Windows 下切换工作目录
- LeetCode Algorithms #1 Two Sum
- 正则表达式:检测密码由6-21字母和数字组成
- Android OpenGL ES 投影矩阵的设置