zoj 1108 FatMouse's Speed 基础dp
2017-08-12 14:54
483 查看
FatMouse's SpeedTime Limit:2 Seconds Memory Limit:65536 KB Special JudgeFatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collectionof 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 SpecificationInput 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 ineach 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 SpecificationYour program should output a sequenceb87fof lines of data; the first line should contain a numbern; the remainingnlines should each contain a single positive integer (each one representing a mouse). If thesen integers arem[1],m[2],...,mthen 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
Output for Sample Input
4 4 5 9 7
天啊,好可耐的老鼠,抱抱。
为达到DP的无后续性,以其中一个为关键词排序,然后最长上升subque。
从后向前不需要倒序输出。从前向后不要忘记倒序输出:
用 数组记录或者递归实现。
#include<cstdio>#include<cstdlib>#include<iostream>#include<algorithm>using namespace std;int m,x,y;struct in{int w,s,pos,pre,num;}a[1001];int b[1001];bool cmp(in a,in b){return a.w<b.w;}void _solve(){for(int i=1;i<=m;i++){for(int j=1;j<i;j++){if(a[i].w>a[j].w&&a[i].s<a[j].s)if(a[i].num<a[j].num+1){a[i].num=a[j].num+1;a[i].pre=j;}}}int M=1,L=1;for(int i=m;i>=1;i--){if(a[i].num>M) {M=a[i].num;L=i;}}printf("%d\n",M);int t=0;while(L>0){//也可以试试调用递归来倒序输出b[++t]=a[L].pos;L=a[L].pre;}for(int i=t;i>=1;i--) printf("%d\n",b[i]);return ;}int main(){while(~scanf("%d%d",&x,&y)){a[++m].w=x;a[m].s=y;a[m].pos=m;a[m].num=1;}sort(a+1,a+m+1,cmp);_solve();return 0;}
相关文章推荐
- ZOJ 1108 FatMouse's Speed【DP】【最大上升子序列】
- ZOJ 1108 FatMouse's Speed(dp+路径还原)
- 【基础dp路径记录】HDU 1160 FatMouse's Speed
- Chapter06-FatMouse‘s Speed(ZOJ 1108)
- ZOJ 1108 FatMouse's Speed
- zoj 1108 FatMouse's Speed(DP)
- J - FatMouse's Speed HDU - 1160——基础dp
- zoj 1108 FatMouse's Speed
- 基础dp9-- J - FatMouse's Speed HDU - 1160
- ZOJ 1108 FatMouse's Speed (HDU 1160) DP
- hdu FatMouse's Speed(DP)(打印路径)
- HDOJ1160 FatMouse's Speed[dp](最长上升子序列)
- hdu 1160 || zoj 1108 FatMouse's Speed
- (HDU - 1160)FatMouse's Speed(DP)
- hdu FatMouse's Speed(DP)(打印路径)
- HDU 1160 FatMouse's Speed(DP)
- FatMouse's Speed(dp)
- hdu FatMouse's Speed(DP)(打印路径)
- ZOJ 1108 FatMouse's Speed
- HDU1160 FatMouse's Speed(DP+路径记录)