您的位置:首页 > 其它

hdu1160 FatMouse's Speed(LIS普通法)

2016-02-26 21:19 357 查看
先以重量升序速度降序排序,求速度的LCS,LCS用路径表示,所以不能用二分法。。。今天才想起我不会普通法。。。 = =学习了。

唯一搞不懂的是为何样例不一样也还是能通过?

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 1005;
const int INF = 1000000;

int n, dp
, pre
;

struct MICE
{
int w, s;
int id;
}mice
;

vector <int> mlen;

bool cmp(MICE x, MICE y)
{
if(x.w == y.w)
return x.s > y.s;
else return x.w < y.w;
}

int main()
{
//  freopen("in.txt", "r", stdin);
int i, j, k, maxl, idm;
n = 0;
while(~scanf("%d%d", &mice
.w, &mice
.s))
{
mice
.id = n + 1;
dp
= 1;
pre
= 0;
n ++;
}
maxl = -1;
mlen.clear();
sort(mice, mice + n, cmp);
for(i = 1; i < n; i ++)
for(j = 0; j < i; j ++)
{
if(mice[j].w < mice[i].w && mice[j].s > mice[i].s && dp[i] < dp[j] + 1)
{
dp[i] = dp[j] + 1;
pre[i] = j;
if(dp[i] > maxl)
{
maxl = dp[i];
idm = i; //最长子序列下标
}
}
}
int x = dp[idm];
printf("%d\n", dp[idm]);
while(idm)
{
mlen.push_back(idm);
idm = pre[idm];
}
for(i = x - 1; i >= 0; i --)
printf("%d\n", mice[mlen[i]].id);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu