给你每个物体两个参数,求最长的链要求第一个参数递增,第二个参数递减,要求输出任意最长路径。

首先第一反应根据第二个参数排个序,然后不就是最长上升子序列的问题吗?

O(nlogn)的复杂度,当然这样可以写,写法也不难。

然后发现这个还是个DAG,也可以用拓扑排序来搞定,输出最长路径,复杂度O(n*n),更新的时候需要更新并记录每个点的前节点,最后倒序输出。

第二种就当练练手吧

先上第二种代码:

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
int d;
struct node
{
int x,y;
}h[1005];
vector<int> t[1005];
int deg[1005];
queue<int> q;
int g[1005];
int ans=0;
int res=-1;
int dp[1005];
vector<int> s;
void tp()
{
for(int i=0;i<d;i++)
{
if(deg[i]==0)
{
dp[i]=1;
q.push(i);
}
}
while(!q.empty())
{
int xx=q.front();
if(dp[xx]>ans)
{
ans=dp[xx];
res=xx;
}
q.pop();
for(int i=0;i<t[xx].size();i++)
{
int w=t[xx][i];
deg[w]--;
if(dp[w]<dp[xx]+1)
{
dp[w]=dp[xx]+1;
g[w]=xx;
}
if(deg[w]==0)
q.push(w);
}
}
} int main()
{
//freopen("input.txt","r",stdin);
int a,b;
memset(g,-1,sizeof(g));
while(scanf("%d%d",&a,&b)==2)
{
h[d].x=a;
h[d].y=b;
d++;
}
for(int i=0;i<d;i++)
for(int j=0;j<d;j++)
{
if(h[i].x<h[j].x&&h[i].y>h[j].y){
t[i].push_back(j);
deg[j]++;
}
}
tp();
printf("%d\n",ans);
s.push_back(res+1);
while(g[res]!=-1)
{
s.push_back(g[res]+1);
res=g[res];
}
int len=s.size();
for(int i=len-1;i>=0;i--)
printf("%d\n",s[i]);
}

第一种代码稍后补上:

参考http://blog.csdn.net/dangwenliang/article/details/5728363

原理:O(N*N)的,一维,设dp[i]为以第i位为结尾的最长长度,dp[i]=max(dp[j])+1(j<i),然后记录前缀,输出路径。

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
int d;
struct node
{
int x,y,r;
}h[1005];
int hh[1005];
int g[1005];
vector<int> t;
bool cmp(node a,node b)
{
if(a.y>b.y)
return 1;
return 0;
}
int f[1005];
int ans,res;
int main()
{
//freopen("input.txt","r",stdin);
int a,b;
memset(g,-1,sizeof(g));
while(scanf("%d%d",&a,&b)==2)
{
h[d].x=a;
h[d].y=b;
h[d].r=d;
d++;
}
sort(h,h+d,cmp);
hh[0]=1;
int res=-1;
ans=0;
for(int i=1;i<d;i++)
{
hh[i]=1;
for(int j=0;j<i;j++)
{
if(h[i].x>h[j].x&&hh[i]<hh[j]+1)
{
hh[i]=hh[j]+1;
g[i]=j;
if(hh[i]>res)
{
ans=i;
res=hh[i];
}
}
}
}
printf("%d\n",res);
t.push_back(ans);
while(g[ans]!=-1)
{
t.push_back(g[ans]);
ans=g[ans];
}
int len=t.size();
for(int i=len-1;i>=0;i--)
{
printf("%d\n",h[t[i]].r+1);
}
}

然后来O(nlogn)算法。

首先能这么做,要满足dp的两个根本原则:1.无后效性,每一个点的状态都由其前面的点决定

                                                       2.每一步都是当前那一个整体最优。

    我们必须用到二分,但是这个前面的点的序列是不连续的,所以我们需要选出前面的点中每一步中的最优,来组成一个"标杆队列",因为对于数和数之间的前后关系是重要的,但对于当前的数,前面的数的前后关系是不重要的,如果每一次二分,dp的同时维护那个"标杆队列",我们可以保证那个队列是最优的,那么下一个点也会是最优的,也可以保证结果是最优的。

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<iostream>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
int d=1;
struct node
{
int x,y,r;
}h[1005];
int ans[1005];
int hh[1005];
int dp[1005];
bool cmp(node a,node b)
{
if(a.y>b.y)
return 1;
else if(a.y==b.y&&a.x>b.x)
return 1;
return 0;
} int main()
{
//freopen("input.txt","r",stdin);
int a,b;
while(scanf("%d%d",&a,&b)==2)
{
h[d].x=a;
h[d].y=b;
h[d].r=d;
d++;
}
sort(h+1,h+d,cmp);
for(int i=1;i<d;i++)
ans[i]=10005;
int len=0;
for(int i=1;i<d;i++)
{
int t=lower_bound(ans+1,ans+d,h[i].x)-ans;
ans[t]=h[i].x;
dp[i]=t;
len=max(len,t);
}
printf("%d\n",len);
int cc=len;
int res=0;
int f=10005;
for(int i=d-1;i>0;i--)
{
if(len==0)
break;
if(dp[i]==len&&h[i].x<f)
{
hh[res++]=i;
len--;
f=h[i].x;
}
}
for(int i=res-1;i>=0;i--)
{
printf("%d\n",h[hh[i]].r);
}
}

zoj1108 FatMouse's Speed的更多相关文章

  1. FatMouse's Speed——J

    J. FatMouse's Speed FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...

  2. HDU 1160 FatMouse's Speed(要记录路径的二维LIS)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. HDU 1160 FatMouse's Speed (DP)

    FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  4. FatMouse's Speed(HDU LIS)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. FatMouse's Speed 基础DP

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. zoj 1108 FatMouse's Speed 基础dp

    FatMouse's Speed Time Limit: 2 Seconds      Memory Limit:65536 KB     Special Judge FatMouse believe ...

  7. J - FatMouse's Speed

    p的思路不一定要到最后去找到ans:也可以设置成在中间找到ans:比如J - FatMouse's Speed 这个题,如果要是让dp[n]成为最终答案的话,即到了i,最差的情况也是dp[i-1],就 ...

  8. HDU 1160:FatMouse's Speed(LIS+记录路径)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. (最长上升子序列 并记录过程)FatMouse's Speed -- hdu -- 1160

    http://acm.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS (Java/Other ...

随机推荐

  1. Foundation--NSDictionary+NSMutableDictionary

    键值对 key(一般为字符串对象)---vaule(必须是对象) Person *p1 =[[Person alloc ]init]; Person *p2 =[[Person alloc ]init ...

  2. .Net中如何使用MySql连接池

    提供一份官方的译文.翻译也挺辛苦的!! 6.4 Using Connector/Net with Connection Pooling 6.4在Connector/Net中使用连接池 The Conn ...

  3. tostring的用法

    ToString()可空参数单独使用,同时可以加一个格式化参数,具体方式如下: . 取中文日期显示_年月 currentTime.ToString("y"); 格式:2007年1月 ...

  4. How to Avoid Producing Legacy Code at the Speed of Typing

    英语不好翻译很烂.英语好的去看原文. About the Author I am a software architect/developer/programmer.I have a rather p ...

  5. 通过一张简单的图,让你搞懂JS的==运算

    == 运算的规则: undefined == null,结果是true.且它俩与所有其他值比较的结果都是false. String == Boolean,需要两个操作数同时转为Number. Stri ...

  6. HDU 5728 - PowMod

    HDU 5728 - PowMod 题意:    定义: k = ∑(i=1,m) φ(i∗n) mod 1000000007 给出: n,m,p ,且 n 无平方因子 求: ans= k^(k^(k ...

  7. ThinkPHP 类似Yii的Gii生成Model的功能。

    ThinkPHP 类似Yii的Gii生成Model的功能.自动生成ThinkPhp 3.1 的基础模型.. #!/usr/bin/env php <?php /** * * THINKPHP 基 ...

  8. MVC4商城项目二:用户身份验证的实现

    用户身份验证,依赖于 forms 身份验证类:FormsAuthentication,它是一串加密的cookie 来实现对控制器访问限制和登陆页面的访问控制.它在浏览器端是这样子的: 需求:我们要实现 ...

  9. 由PhysicalFileProvider构建的物理文件系统

    由PhysicalFileProvider构建的物理文件系统 ASP.NET Core应用中使用得最多的还是具体的物理文件,比如配置文件.View文件以及网页上的静态文件,物理文件系统的抽象通过Phy ...

  10. What does cmd /C mean? [closed] 关于nodejs的子进程部分

    之前一直很不明白为什么 child_process.spawn(command[, args][, options]) shell <Boolean> | <String> I ...