开始以为是贪心,10分;想了个DP估计会超时,一翻题解各路初中神仙,背包都有。

\(n^2\)很好想,考虑单调性用二分优化出log

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long #define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 150007; struct Field{
int l, r, w;
bool operator < (const Field &com) const{
if(r != com.r) return r < com.r;
return l < com.l;
}
}a[N]; int f[N]; inline int Find(int x){
int l = 1, r = x;
int ans = -1;
while(l <= r){
int mid = (l + r) >> 1;
if(a[mid].r < a[x].l){
l = mid + 1;
ans = mid;
}
else{
r = mid - 1;
}
}
return ans;
}
int main(){
//FileOpen();
int n;
io >> n;
R(i,1,n){
io >> a[i].l >> a[i].r;
a[i].w = a[i].r - a[i].l + 1;
} sort(a + 1, a + n + 1); f[1] = a[1].r - a[1].l + 1;
R(i,2,n){
int j = Find(i);
if(j != -1)
f[i] = Max(f[i - 1], f[j] + a[i].w);
else
f[i] = Max(f[i - 1], a[i].w);
} printf("%d\n",f[n]); return 0;
}

最新文章

  1. Codeigniter 3.0 相关文档 part two
  2. Javascript 代码格式化(JsFormat)
  3. chosen PersistenceUnitInfo does not specify a provider class name
  4. app控件获取之uiautomatorviewer
  5. matlab实现共轭梯度法、多元牛顿法、broyden方法
  6. utf-8转换为ansi和修改文件名的批处理(可解决source insight中文注释乱码问题)
  7. 高速排序-c++(分别用数组和容器实现)
  8. time date 基础操作
  9. java array to list
  10. Python——Label控件说明
  11. windows_agent 添加
  12. AtCoder Grand Contest 026 (AGC026) E - Synchronized Subsequence 贪心 动态规划
  13. 【Java基础】10、Java中throw和throws的区别
  14. Ubuntu下安装和使用zookeeper和kafka
  15. LibreOJ 6282. 数列分块入门 6
  16. oauth2.0服务端与客户端搭建
  17. prefix pch 中引用cocoapods 中的头文件失败
  18. HIVE学习(待更新)
  19. 【转】用Jmeter制造测试数据
  20. 浅述Try {} Catch{} 作用

热门文章

  1. Win10系统下怎么让局域网内其他电脑通过IP访问网站
  2. 用t-SNE进行流形学习(digits数据集)
  3. 题解 P3831 [SHOI2012]回家的路
  4. SpringBoot+Mybatis-Plus整合Sharding-JDBC5.1.1实现单库分表【全网最新】
  5. 换个角度带你学C语言的基本数据类型
  6. 用STM32玩SR04(测距、串口显示、OLED显示)
  7. 【Redis】字典
  8. 【Redis】客观下线
  9. Redis的内存淘汰策略(八)
  10. 关于Vue移动端框架(Muse-UI)的使用(说明书,针对不愿看文档的童鞋)