zoj 1108 FatMouse's Speed 基础dp

来源:互联网 发布:移动网络转换器 编辑:程序博客网 时间:2024/05/16 13:47
FatMouse's Speed

Time Limit: 2 Seconds      Memory Limit:65536 KB     Special Judge

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 Specification

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 Specification

Your program should output a sequence of lines of data; the first line should contain a numbern; the remainingn lines should each contain a single positive integer (each one representing a mouse). If thesen integers arem[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.

Sample Input

6008 13006000 2100500 20001000 40001100 30006000 20008000 14006000 12002000 1900

Output for Sample Input

44597
天啊,好可耐的老鼠,抱抱。
为达到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;}

原创粉丝点击