URAL - 1078 Segments
题目大意:有n条线段,一个线段a 完全覆盖另一个线段b 当且仅当,a.l < b.l && a.r>b.r。问你
一个线段覆盖一个线段再覆盖一个线段再.......,问你最多几个线段属于这种关系,并打印出路径。
这题的的 n 太小了,n^3的方法都能过。
思路:1. 我们设dp[ i ] 为 以 i 位最外层的答案为多少,n^3的方法是,我们先将所有dp的值设为1,
枚举最外层的点,再枚举其内层的点更新,一次更新肯定是不够的,我们更新到它没有更新了再
退出,感觉和最短路的贝尔曼算法相似,这种方法显然复杂度很高。
2.第一种方法中我们显然是无脑地枚举边,怎么才能减少不必要的操作呢。我们将边按左端点的
大小排序,然后从后往前枚举最外层的点,因为是按左端点排序的而且是从后往前枚举,所以能
被当前点包含的肯定在该点的后面,这样我们每次更新当前点之后,当前点的dp值肯定是最优值,
这样我们就将复杂度降到了n^2。(虽然我排了序,但我是傻逼从前往后枚举,虽然优化了一点,还是n^3)
3.这是一个学姐的方法,也是复杂度应该也是n^2,我们将每条线段都看成一个点,然后如果一条
线段能包含另一条线段,我们就在他们之间连一条有向边。那么我们就从各个点出发,看每个点
到最深处的路径长度是多少,显然也能用记忆化搜索。(这个方法我感觉好牛逼啊)
这里贴一下第二种方法的代码吧QAQ
#include<bits/stdc++.h>
using namespace std;
const int N=;
int n,pre[],dp[];
struct node
{
int fi,se,id;
bool operator < (const node &t)const
{
return fi<t.fi;
}
}seg[];
int main()
{
cin>>n;
for(int i=;i<=n;i++) scanf("%d%d",&seg[i].fi,&seg[i].se),seg[i].id=pre[i]=i;
sort(seg+,seg+n+);
for(int i=;i<=n;i++) dp[i]=;
for(int i=n;i>=;i--)
{
for(int j=i+;j<=n;j++)
{
if(seg[j].fi>seg[i].fi && seg[j].se<seg[i].se && dp[i]<dp[j]+)
{
dp[i]=dp[j]+;
pre[i]=j;
}
}
}
int mx=,item;
for(int i=;i<=n;i++)
{
if(dp[i]>mx)
{
mx=dp[i];
item=i;
}
}
cout<<mx<<endl;
vector<int> ans;
ans.push_back(seg[item].id);
while(item!=pre[item])
{
item=pre[item];
ans.push_back(seg[item].id);
}
int len=ans.size();
for(int i=len-;i>=;i--) printf("%d%c",ans[i],i== ? '\n':' ');
return ;
}
URAL - 1078 Segments的更多相关文章
- URAL(DP集)
这几天扫了一下URAL上面简单的DP 第一题 简单递推 1225. Flags #include <iostream> #include<cstdio> #include< ...
- ural 1273. Tie
1273. Tie Time limit: 1.0 secondMemory limit: 64 MB The subway constructors are not angels. The work ...
- URAL 1776 C - Anniversary Firework DP
C - Anniversary FireworkTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/c ...
- ural 1303 Minimal Coverage【贪心】
链接: http://acm.timus.ru/problem.aspx?space=1&num=1303 http://acm.hust.edu.cn/vjudge/contest/view ...
- [LeetCode] Number of Segments in a String 字符串中的分段数量
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...
- Greenplum记录(一):主体结构、master、segments节点、interconnect、performance monitor
结构:Client--master host--interconnect--segment host 每个节点都是单独的PG数据库,要获得最佳的性能需要对每个节点进行独立优化. master上不包含任 ...
- Application package 'AndroidManifest.xml' must have a minimum of 2 segments.
看了源码就是packagename里面必须包含一个. 源码在: ./sdk/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/id ...
- segments&cache
Segments 执行效果 命令 在 sense 里边执行 GET /abcd/_segments 前边的是索引名称,后边是请求 段信息 说明 索引是面向分片的,是由于索引是由一个或多个分片( ...
- BZOJ 1078: [SCOI2008]斜堆
1078: [SCOI2008]斜堆 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 770 Solved: 422[Submit][Status][ ...
随机推荐
- 一个奇怪的SystemClock_Config问题解决方法
一直在SystemClock_Config程序死掉,然后下载最新的STm32Cube从新创建项目成功了.
- EventKey为last_trade_no的subscribe关注事件
如果用户曾经在该公众号有支付行为,关注的时候EventKey中将包含上次交易订单号,如 last_trade_no_4002752001201704258347703919 <xml> & ...
- codeforces724G Xor-matic Number of the Graph
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- C# 面向对象的new关键字的使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- Java并发编程--并发容器之Collections
在JDK1.2之前同步容器类包括Vector.Hashtable,这两个容器通过内置锁synchronized保证了同步.后面的ArrayList.LinkedList.HashMap.LinkedH ...
- nginx入门三
负载均衡 upstream upstream app_server { server 127.0.0.1:8000; server 192.168.2.134:80; server 47.xx.xx. ...
- CentOS 6.8 部署django项目一
CentOS 6.8 部署django项目二 1.安装python3.5(默认是2.6) 参考:http://blog.csdn.net/shaobingj126/article/details/50 ...
- 生成eps图形
(1) matlab可直接将生成图片保存为eps格式. print -fhandle -rresolution -dfileformat filename 例子:set(gcf,'paperposit ...
- 使用PowerMockito和Mockito进行模拟测试,包括静态方法测试,私有方法测试等,以及方法执行的坑或者模拟不成功解决
依赖:这个很重要,不同版本用法也有点区别: <dependency> <groupId>org.mockito</groupId> <artifactId&g ...
- python中argparse模块用法实例详解
python中argparse模块用法实例详解 这篇文章主要介绍了python中argparse模块用法,以实例形式较为详细的分析了argparse模块解析命令行参数的使用技巧,需要的朋友可以参考下 ...