HDU 1160 FatMouse’s Speed(DP)

來源:互聯網
上載者:User

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1160

這個是一個很水的DP 了,複雜度O(n^2)的

首先先按照weight排序下,然後開始DP

dp方程很簡單:dp[i]=MAX(dp[j]+1])           (j>0 && j<i && po[i].speed < po[j].speed && po[i].weight > po[j].weight)

#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>using namespace std;struct point{    int weight;    int speed;    int num;}po[1500];int dp[1500];int pos[1500];int ans[1500];int DP(int n){    dp[0]=1;    int i,j,k,num;    for(i=1;i<n;i++)    {    k=0,num=0;    for(j=0;j<i;j++)    if(po[i].speed < po[j].speed && po[i].weight > po[j].weight && dp[j]>k)    k=dp[j],num=j;    dp[i]=k+1;    pos[i]=num;    }    num=0,k=0;    for(i=0;i<n;i++)    if(dp[i] > k)    k=dp[i],num=i;    printf("%d\n",k);    k=0;    while(pos[num]!=0)    {        ans[k++]=po[num].num+1;        num=pos[num];    }    ans[k]=po[num].num+1;    for(i=k;i>=0;i--)    printf("%d\n",ans[i]);    return 0;}bool cmp(const point &a,const point &b){    return a.weight < b.weight;}int main(){    int i,j;    i=0;    memset(pos,0,sizeof(pos));    while(scanf("%d%d",&po[i].weight,&po[i].speed)!=EOF)    po[i].num=i,i++;    sort(po,po+i,cmp);    //for(j=0;j<i;j++)    //printf("%d\n",po[j].weight);    DP(i);    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.