J - FatMouse's Speed HDU 1160 (动态规划,最长上升子序列+路径输出)
2015-04-24 18:18
405 查看
J - FatMouse's Speed
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1160
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
对速度进行排序,然后对体重来一遍最长上升子序列,同时还要记录路径
dp[i]:表示已i结尾的最长上升子序列,转移是由 前面的加一后的最大值,dp[i]=max(d[i],dp[j]+1);
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 1160
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
对速度进行排序,然后对体重来一遍最长上升子序列,同时还要记录路径
dp[i]:表示已i结尾的最长上升子序列,转移是由 前面的加一后的最大值,dp[i]=max(d[i],dp[j]+1);
#include<bits/stdc++.h> using namespace std; template<class T>inline T read(T&x) { char c; while((c=getchar())<=32)if(c==EOF)return 0; bool ok=false; if(c=='-')ok=true,c=getchar(); for(x=0; c>32; c=getchar()) x=x*10+c-'0'; if(ok)x=-x; return 1; } template<class T> inline T read_(T&x,T&y) { return read(x)&&read(y); } template<class T> inline T read__(T&x,T&y,T&z) { return read(x)&&read(y)&&read(z); } template<class T> inline void write(T x) { if(x<0)putchar('-'),x=-x; if(x<10)putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } template<class T>inline void writeln(T x) { write(x); putchar('\n'); } //-------ZCC IO template------ const int maxn=1e5+1000; const double inf=999999999; #define lson (rt<<1),L,M #define rson (rt<<1|1),M+1,R #define M ((L+R)>>1) #define For(i,t,n) for(int i=(t);i<(n);i++) typedef long long LL; typedef double DB; typedef pair<int,int> P; #define bug printf("---\n"); #define mod 100007 struct node { int weight; int id; int speed; bool operator<(const node tmp) const { if(speed!=tmp.speed)return speed>tmp.speed; return weight<tmp.weight; } } p[maxn]; int pre[maxn]; int dp[maxn]; void to(int x) { if(x==-1)return ; to(pre[x]); printf("%d\n",p[x].id); } int main() { int n; int i=0; while(read_(p[i].weight,p[i].speed)) { p[i].id=i+1; pre[i]=-1; dp[i]=0; i++; } n=i; sort(p,p+n); // for(int i=0;i<n;i++) // printf("%d %d %d\n",p[i].weight,p[i].speed,p[i].id); // printf("\n"); int N=0,ans=0; for(int i=0; i<n; i++) { dp[i]=1; for(int j=0; j<i; j++)if(p[j].weight<p[i].weight) { if(dp[i]<dp[j]+1) { dp[i]=dp[j]+1; pre[i]=j; } } if(N<dp[i]) { ans=i;//记下最后的下标,便于路径输出 N=dp[i]; } } printf("%d\n",N); to(ans);//路径输出 return 0; }
相关文章推荐
- hdu 1160 FatMouse's Speed(最长递减子序列+输出路径)
- HDU 1160 FatMouse's Speed (最长上升子序列+记录路径)
- hdu 1160 FatMouse's Speed(最大上升子序列+路径输出)
- 【最长上升子序列 && 输出路径】HDU - 1160 FatMouse's Speed
- HDU 1160 FatMouse's Speed (最长上升子序列+记录路径)
- HDU1160 FatMouse's Speed 带路径的最长上升子序列问题
- HDU 1160 FatMouse's Speed (最长上升子序列+路径输出)
- hdu 1160 FatMouse's Speed(最长上升子序列路径输出)
- HDU 1160 FatMouse's Speed (最长有序的上升子序列)
- hdu 1160 FatMouse's Speed(最长上升子序列 +记录路径)
- HDU 1160 FatMouse's Speed(最长上升子序列长度及其路径)
- HDU_1160_FatMouse's Speed(最长递增子序列变形+记录路径)
- HDU 1160 FatMouse's Speed (最长有序的上升子序列)
- HDU1160 - FatMouse's Speed(最长下降子序列+打印路径)
- hdu 1160 FatMouse's Speed(最大上升子序列dp)
- HDU 1160 FatMouse's Speed(严格最长递减序列变形+输出)【输出路径模板】
- hdu1160 FatMouse's Speed 最长上升子序列 LIS
- HDU NO.1160 FatMouse's Speed(“最长上升子序列”,记录路径)
- hdu-1160 FatMouse's Speed 【最长上升子序列】
- hdu1160 FatMouse's Speed 【最长下降子序列+输出】