Codevs 3012 线段覆盖 4
3012 线段覆盖 4
时间限制: 1 s
空间限制: 64000 KB
题目等级 : 黄金 Gold
题目描述 Description
数轴上有n条线段,线段的两端都是整数坐标,坐标范围在0~1000000,每条线段有一个价值,请从n条线段中挑出若干条线段,使得这些线段两两不覆盖(端点可以重合)且线段价值之和最大。
输入描述 Input Description
第一行一个整数n,表示有多少条线段。
接下来n行每行三个整数, ai bi ci,分别代表第i条线段的左端点ai,右端点bi(保证左端点<右端点)和价值ci。
输出描述 Output Description
输出能够获得的最大价值
样例输入 Sample Input
3
1 2 1
2 3 2
1 3 4
样例输出 Sample Output
4
数据范围及提示 Data Size & Hint
n <= 1000000
0<=ai,bi<=1000000
0<=ci<=1000000
数据输出建议使用long long类型(Pascal为int64或者qword类型)
分类标签 Tags
二分法 动态规划 序列型DP
/*
DP.
f[i]表示选前i个线段的最优值.
然后DP选不选该线段.
我们保证f值单调.
然后我们从n^2优化到nlogn.
如果从前边转移的话.
按照右端点排序.
找一个合法最近的线段更新.
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#define MAXN 1000001
#define LL long long
using namespace std;
LL f[MAXN],n,tot;
struct data{LL x,y,z;}s[MAXN];
LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
bool cmp(const data &x,const data &y)
{
return x.y<y.y;
}
LL erfen(LL l,LL p)
{
LL mid,ans,r=p-1;
while(l<=r)
{
mid=(l+r)>>1;
if(s[mid].y<=s[p].x)
ans=mid,l=mid+1;
else r=mid-1;
}
return ans;
}
int main()
{
LL x,y,z;
n=read();
for(int i=1;i<=n;i++)
s[i].x=read(),s[i].y=read(),s[i].z=read();
sort(s+1,s+n+1,cmp);
for(int i=1;i<=n;i++)
f[i]=max(f[i-1],f[erfen(0,i)]+s[i].z),tot=max(tot,f[i]);
printf("%lld",tot);
return 0;
}
/*
按照左端点排序.
从后面更新状态.
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#define MAXN 1000001
#define LL long long
using namespace std;
LL f[MAXN],n,tot;
struct data{LL x,y,z;}s[MAXN];
LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*f;
}
bool cmp(const data &x,const data &y)
{
return x.x<y.x;
}
LL erfen(LL p)
{
LL mid,ans,l=p+1,r=n+1;
while(l<=r)
{
mid=(l+r)>>1;
if(s[mid].x>=s[p].y)
ans=mid,r=mid-1;
else l=mid+1;
}
return ans;
}
int main()
{
LL x,y,z;
n=read();
for(int i=1;i<=n;i++)
s[i].x=read(),s[i].y=read(),s[i].z=read();
sort(s+1,s+n+1,cmp);
for(int i=n;i>=1;i--)
f[i]=max(f[i+1],f[erfen(i)]+s[i].z),tot=max(tot,f[i]);
printf("%lld",tot);
return 0;
}
Codevs 3012 线段覆盖 4的更多相关文章
- codevs 3012 线段覆盖4
传送门 3012 线段覆盖 4 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 数轴上有n条线段,线段的两端都是整数坐标,坐 ...
- codevs 3012 线段覆盖 4 & 3037 线段覆盖 5
3037 线段覆盖 5 时间限制: 3 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 数轴上有n条线段,线段的两端都 ...
- codevs 1214 线段覆盖
1214 线段覆盖 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定x轴上的N(0<N<100)条线段,每个线段 ...
- codevs 1214 线段覆盖/1643 线段覆盖 3
1214 线段覆盖/1214 线段覆盖 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定x轴上的N(0< ...
- codevs 1643 线段覆盖 3
1643 线段覆盖 3 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 在一个数轴上有n条线段,现要选取其中 ...
- CODEVS 3027 线段覆盖2
首先,先看题.....(虽然比较简单 3027 线段覆盖 2 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description 数轴上有n条线段,线段的两端都是整数坐标,坐 ...
- codevs 3027线段覆盖2
传送门 3027 线段覆盖 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 数轴上有n条线段,线段的两端都是整数坐标, ...
- codevs 1214线段覆盖
1214 线段覆盖 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定x轴上的N(0<N<100)条线段,每 ...
- codevs——T1214 线段覆盖
http://codevs.cn/problem/1214/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descr ...
随机推荐
- XML工具——xmlbeans的使用
一.安装xmlbeans 1.下载xmlbeans 下载地址:https://gitee.com/shizuru/xmlbeans-2.6.0 2.解压,此处以解压至D盘根目录为例 3.配置环境变量( ...
- 开始学Python 啦 ,持续不断总结中。。(转)快捷键的使用
最重要的快捷键1. ctrl+shift+A:万能命令行2. shift两次:查看资源文件新建工程第一步操作1. module设置把空包分层去掉,compact empty middle packag ...
- C# 委托 、事件、同步、异步知识点归纳
一.委托 基本用法: 1.声明一个委托类型.委托就像是‘类'一样,声明了一种委托之后就可以创建多个具有此种特征的委托.(特征,指的是返回值.参数类型) public delegate void Som ...
- No database provider has been configured for this DbContext
var context = ((IInfrastructure<IServiceProvider>)set).GetService<DbContext>(); 在EF Core ...
- python 把函数的值赋给变量
本博文的知识点一个是模块的调用和一个自定义函数返回值赋值给变量 编写一个简单的函数模块: [root@bigdata zw]# more d.py #!/usr/bin/python # -*- co ...
- 简单服务器通信 模型socketserver
硬件服务器:主机 集群 厂商 :IBM HP 联想 浪潮 软件服务器 :编写的服务端应用程序,在硬件服务器上运行,一般依托于操作系统,给用户提供一套完整的服务 httpserver --> ...
- OpenCV视觉处理核心课程
OpenCV视觉处理核心课程 观看链接:https://www.bilibili.com/video/av29500928?from=search&seid=47008639320014639 ...
- redis—持久化操作
简介 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(d ...
- 8. Object References, Mutability, and Recycling
1. Variables Are Not Boxes # Think variables as sticky notes a = [1, 2, 3] b = a a.append(4) print b ...
- C++ 内存泄露和内存越界
内存泄露:分配了内存而没有释放,逐渐耗尽内存资源,导致系统崩溃内存越界: 打个比方 就是你有一个500ml的水瓶,然后你倒在瓶里的水大于500ml 那个多余的就会流出来... 1. 原理分析经常有些新 ...