codevs 3037 线段覆盖 5,codevs 3012 线段覆盖 4,codevs 3027 线段覆盖 2
来源:互联网 发布:中走丝hf锥度编程视频 编辑:程序博客网 时间:2024/06/05 06:07
题目描述 Description
数轴上有n条线段,线段的两端都是整数坐标,坐标范围在0~10^18,每条线段有一个价值,请从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<=10^18
0<=ci<=10^9
数据输出建议使用long long类型(Pascal为int64或者qword类型)
数据较大,我们可以用二分来优化
#include <cstdio>#include <algorithm>#include <cstring>#include<iostream>using namespace std;const int MAXN = 1000000 + 2;typedef long long ll;struct Line{ ll l,r,v;}lines[MAXN];bool cmp(Line a, Line b){ return a.r < b.r;}int find(int x) //二分 { int l = 1, r = x, mid = 0; int ans = 0; while(l <= r) { mid = (l + r) >> 1; if (lines[mid].r > lines[x].l) { r = mid - 1; } else { ans = mid; l = mid + 1; } } return ans;}ll f[MAXN];int main(){ int n = 0; scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%lld%lld%lld", &lines[i].l, &lines[i].r, &lines[i].v); sort(lines + 1, lines + n + 1,cmp); for(int i = 1; i <= n; ++i) f[i] = max(f[i - 1], f[find(i)] + lines[i].v); printf("%lld", f[n]); return 0;}
4 0
- codevs 3037 线段覆盖 5,codevs 3012 线段覆盖 4,codevs 3027 线段覆盖 2
- 线段覆盖2 CODEVS
- CodeVS 3012 & 3037 线段覆盖4 & 5
- 【codevs 3012】线段覆盖 4
- Codevs 3012 线段覆盖 4
- CODEVS 3012 线段覆盖 4
- [CODEVS 3037] 线段覆盖 5
- 【Codevs 3037】线段覆盖5
- Codevs 3027 线段覆盖2
- CodeVS 3027 线段覆盖2
- 【Codevs 3027】线段覆盖2
- CODEVS 3027 线段覆盖 2
- Codevs 3027 线段覆盖 2
- codevs 1214 线段覆盖
- 【codevs 1214】线段覆盖
- Codevs 1214 线段覆盖
- CODEVS 1214线段覆盖
- codeVS 1214 线段覆盖
- Qt使用MySQL库
- Linux网络IO并行化技术概览
- poj 1836 Alignment 最长递增子序列
- Hive – Group By 的实现
- latex参考文献BibTeX的使用方法
- codevs 3037 线段覆盖 5,codevs 3012 线段覆盖 4,codevs 3027 线段覆盖 2
- vue项目的搭建
- 第十周 项目二-遍历二叉树
- 在Eclipse中运行的时候出现launching/Building
- Redis安装和远程访问
- android 布局参数理解
- Android日期选择器可以设置起始日期和终止日期
- 第十周项目2—二叉树遍历的递归算法
- 第10周 项目1-二叉树的算法库