基础dp9-- J

来源:互联网 发布:手机淘宝店名怎么改 编辑:程序博客网 时间:2024/06/06 06:52

题意:
有若干只老鼠,给出每只老鼠的大小和速度。输出尽量多的老鼠的下标m1,m2,m3……满足下标对应的老鼠大小严格递增而老鼠速度严格递减。

思路:先对老鼠的速度从大到小排序,在对老鼠的大小求最长上升子序列。在这过程中,用pre[ ]记录路径。

注意:答案与示例不一致,题目要求是只要输出一种即可,不需要和题目中的一定相同
code

#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<queue>#include<algorithm>#include<iostream>#include<set>#include<iomanip>using namespace std;int n,m,r,k;int mmax;int a[111111];int tu[1130][1120];//int numint tx[]={0,1,0,-1};int ty[]={1,0,-1,0};struct node{    int speed,weight,id;    bool operator<(const node&t)    {        if(speed==t.speed)            return weight<t.weight;        else            return speed>t.speed;    }}data[9999];int dp[1200],pre[1200];int cnt,s,w,mm,mmid;void solve(){    memset(pre,-1,sizeof(pre));    for(int i=1;i<=cnt;i++)    {        dp[i] = 1;        for(int j=1;j<i;j++)        {            if(data[j].weight<data[i].weight&&dp[i]<dp[j]+1)            {                dp[i] = dp[j]+1;                pre[data[i].id] = data[j].id;               //  mmid = data[i].id;            }        }        if(mm<dp[i])        {            mm = dp[i];            mmid = data[i].id;        }    }    int top = 0;    cout<<mm<<endl;    int temp[1000];    for(int i=mmid;i!=-1;i=pre[i])    {        temp[++top] = i;    }    for(int i=top;i>=1;i--)    {        printf("%d\n",temp[i]);    }}int main(){     cnt = 0;     while(cin>>w>>s)     {         node t;         t.speed = s;         t.weight = w;         t.id = cnt+1;         data[++cnt] = t;     }      mm = 0;         sort(data+1,data+1+cnt);         solve();     return 0;}
原创粉丝点击