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的更多相关文章

  1. codevs 3012 线段覆盖4

    传送门 3012 线段覆盖 4  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 黄金 Gold   题目描述 Description 数轴上有n条线段,线段的两端都是整数坐标,坐 ...

  2. codevs 3012 线段覆盖 4 & 3037 线段覆盖 5

    3037 线段覆盖 5  时间限制: 3 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 数轴上有n条线段,线段的两端都 ...

  3. codevs 1214 线段覆盖

    1214 线段覆盖 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold   题目描述 Description 给定x轴上的N(0<N<100)条线段,每个线段 ...

  4. codevs 1214 线段覆盖/1643 线段覆盖 3

    1214 线段覆盖/1214 线段覆盖  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold       题目描述 Description 给定x轴上的N(0< ...

  5. codevs 1643 线段覆盖 3

    1643 线段覆盖 3  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 在一个数轴上有n条线段,现要选取其中 ...

  6. CODEVS 3027 线段覆盖2

    首先,先看题.....(虽然比较简单 3027 线段覆盖 2    时间限制: 1 s  空间限制: 128000 KB 题目描述 Description 数轴上有n条线段,线段的两端都是整数坐标,坐 ...

  7. codevs 3027线段覆盖2

    传送门 3027 线段覆盖 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 数轴上有n条线段,线段的两端都是整数坐标, ...

  8. codevs 1214线段覆盖

    1214 线段覆盖  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 给定x轴上的N(0<N<100)条线段,每 ...

  9. codevs——T1214 线段覆盖

    http://codevs.cn/problem/1214/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descr ...

随机推荐

  1. Let's Code

    Let's Code Happy Coding, Happy OI #include <bits/stdc++.h> using namespace std; int main() { c ...

  2. Python中的with语句(上下文管理协议)

    在平时工作中总会有这样的任务,它们需要开始前做准备,然后做任务,然后收尾清理....比如读取文件,需要先打开,读取,关闭 这个时候就可以使用with简化代码,很方便 1.没有用with语句 f = o ...

  3. OpsManager管理MongoDB

    mydb1 Ops Manager,mongodb,agent mydb2 mongodb,agent mydb3 mongodb,agent NUMA Settings sysctl -w vm.z ...

  4. Python 变量作用域与函数

    Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...

  5. Idea 快捷生成方法(待完善)

    1.System.out.println() 输入sout,按下enter键,生成System.out.println()方法. sout--->soutv=System.out.println ...

  6. MyBatis 源码篇-插件模块

    本章主要描述 MyBatis 插件模块的原理,从以下两点出发: MyBatis 是如何加载插件配置的? MyBatis 是如何实现用户使用自定义拦截器对 SQL 语句执行过程中的某一点进行拦截的? 示 ...

  7. C#字典转对象

    /// <summary> /// Assign parameters to specified objects /// </summary> /// <typepara ...

  8. [转载]Pytorch详解NLLLoss和CrossEntropyLoss

    [转载]Pytorch详解NLLLoss和CrossEntropyLoss 来源:https://blog.csdn.net/qq_22210253/article/details/85229988 ...

  9. seaborn:一个更强大的画图工具

    数据加载: 1. Countplot 和之前的pandas绘图相比,使用countplot可以自动计算每类的数量. 2. KDE Plot KDE,是"kernel density esti ...

  10. Flutter 34: 图解自定义 View 之 Canvas (一)

    小菜最近在学习自定义 View,刚了解了一下 Paint 画笔的神奇之处,现在学习一下 Canvas 画布的神秘之处.Flutter 提供了众多的绘制方法,小菜接触不深,尽量都尝试一下. Canvas ...