hdu 1160 dp (二维最长上升子序列 记录路径
2017-08-30 11:11
441 查看
传送门
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.
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
then it must
be the case that
W[m[1]] < W[m[2]] < ... < W[m
]
and
S[m[1]] > S[m[2]] > ... > S[m
]
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 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
Sample Output
4
4
5
9
7
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递推出来的。所以可以用这种方法求的最优路径。
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.
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
then it must
be the case that
W[m[1]] < W[m[2]] < ... < W[m
]
and
S[m[1]] > S[m[2]] > ... > S[m
]
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 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
Sample Output
4
4
5
9
7
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 long typedef long long LL; const 4000 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); } }
相关文章推荐
- hdu 1160 dp (二维最长上升子序列 记录路径
- hdu 1160 dp (二维最长上升子序列 记录路径
- hdu 1160 dp (二维最长上升子序列 记录路径
- hdu 1160 dp (二维最长上升子序列 记录路径
- hdu 1160 dp (二维最长上升子序列 记录路径
- hdu 1160 dp (二维最长上升子序列 记录路径
- hdu 1160 dp (二维最长上升子序列 记录路径
- hdu 1160 dp (二维最长上升子序列 记录路径
- hdu 1160 dp (二维最长上升子序列 记录路径
- hdu 1160 dp (二维最长上升子序列 记录路径
- hdu 1160 dp (二维最长上升子序列 记录路径
- hdu 1160 dp (二维最长上升子序列 记录路径
- HDU 1160 FatMouse's Speed 最长上升子序列及记录路径DP
- HDU 1160 FatMouse's Speed (最长上升子序列+记录路径)
- 【最长上升子序列记录路径(n^2)】HDU 1160 FatMouse's Speed
- HDU 1160 FatMouse's Speed (最长上升子序列+记录路径)
- HDU 1160(记录路径的最长上升子序列)
- hdu 1160 最长上升子序列 + 记录路径
- HDU_1160_FatMouse's Speed(最长递增子序列变形+记录路径)
- hdu 1160 FatMouse's Speed(最长上升子序列路径输出)