您的位置:首页 > 其它

HDU 1160 FatMouse's Speed

2013-04-07 15:33 246 查看
/**
*   Author : johnsondu
*   time : 2013-4-7
*   Type: DP, I want to really understand it
*   problem: FatMouse's Speed
*   url: http://acm.hdu.edu.cn/showproblem.php?pid=1160 */

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std ;

#define max(a, b) (a > b ? a : b)
struct Node
{
int w, s ;
int idx ;
}node[1005] ;
int n, dp[1005] ;

bool cmp (const Node &a, const Node &b)
{
if (a.w != b.w)
return a.w < b.w ;
return a.s > b.s ;
}

int main ()
{
int i, j ;
//freopen ("data.txt", "r", stdin) ;
n = 0 ;
while (scanf ("%d%d", &node
.w, &node
.s) != EOF)
{
node
.idx = n ;
n ++ ;
}
sort (node, node + n, cmp) ;

for (i = 0; i < n; i ++)
dp[i] = 1 ;

int num = 0 ;
for (i = 0; i < n-1; i ++)
{
for (j = i+1; j < n; j ++)
if (node[j].w > node[i].w && node[j].s < node[i].s)
{
dp[j] = max(dp[i]+1, dp[j]) ;           //状态转移方程
if (dp[j] > dp[num])
num = j ;                           //记录最大值下标
}
}
printf ("%d\n", dp[num]) ;
int tp = num ;
vector<int> mm ;
mm.push_back(num) ;
for (i = num-1; i >= 0; i --)   //寻找路径
if (dp[num] == dp[i] + 1)
{
mm.push_back(i) ;
num = i ;
}
for (i = dp[tp]-1; i >= 0; i --)
printf ("%d\n", node[mm[i]].idx+1) ;

return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: