CF1399F Yet Another Segments Subset
首先注意一下题面要求,使得选出的线段两两要么包含要么不相交,也就是说一条线段可能会出现不相交的几条线段,而这些线段上面也可能继续这样包含线段。然后我们可以发现我们要做的实际上是在这条线段上选取几条线段然后递归求出子问题,这是一个 \(dp\) 的形式,令 \(f_i\) 表示在线段 \(i\) 上最多能选出多少个满足条件的区间。但是在这里拓扑序我们还未知,我们看看能否求出这个 \(dp\) 的拓扑序。我们发现对于任意一条线段我们能选择被它包含的线段必然左端点大于该线段左端点,右端点大于该线段左端点,那么我们只需要按照右端点排序,那么每次能转移到的区间就都会已经计算完毕。但需要注意的是如果右端点相同,我们还要按照左端点从大到小排序,这样才能保证小的线段先算。
接下来考虑如何转移,实际上这里需要求解的一个问题就是每个线段有一个权值,选出一些不相交的线段使得它们的权值最大。接下来可以发现如果当前线段能选,那么上次选的最右边线段的右端点一定要比当前线段的左端点要靠前,于是可以令 \(dp_i\) 表示当前选择的最右边的右端点在 \(i\) 的最大权值,有转移:
\]
将 \(f_i\) 拉出来这就是一个前缀 \(\max\) 的形式,因为右端点是单调递增的,因此只需要沿途维护一个指针扫过来并统计即可。本题当中值域很大?可以发现我们只关注大小关系,离散化即可。
#include<bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for(int i = l; i <= r; ++i)
const int N = 6000 + 5;
struct node{
int l, r;
}a[N];
int T, n, tot, cnt, f[N], g[N], d[N], dp[N];
int read(){
char c; int x = 0, f = 1;
c = getchar();
while(c > '9' || c < '0'){ if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
bool cmp(node a, node b){
return a.r == b.r ? a.l > b.l : a.r < b.r;
}
int main(){
T = read();
while(T--){
n = read(), tot = 0;
rep(i, 1, n) a[i].l = read(), a[i].r = read(), d[++tot] = a[i].l, d[++tot] = a[i].r;
sort(d + 1, d + tot + 1), sort(a + 1, a + n + 1, cmp);
cnt = unique(d + 1, d + tot + 1) - d - 1;
rep(i, 1, n){
a[i].l = lower_bound(d + 1, d + cnt + 1, a[i].l) - d;
a[i].r = lower_bound(d + 1, d + cnt + 1, a[i].r) - d;
}
rep(i, 1, n){
int P = 0, l = 0;
rep(j, 1, a[i - 1].r) dp[j] = g[j] = 0;
rep(j, 1, i - 1) if(a[j].l >= a[i].l){
if(a[j].r != l) ++P;
while(P < a[j].r) g[P] = g[P - 1], ++P;
dp[a[j].r] = max(dp[a[j].r], g[a[j].l - 1] + f[j]), g[P] = max(g[P - 1], dp[a[j].r]);
l = a[j].r;
}
f[i] = g[P] + 1;
}
int P = 0, l = 0;
rep(i, 1, a[n].r) dp[i] = g[i] = 0;
rep(i, 1, n){
if(a[i].r != l) ++P;
while(P < a[i].r) g[P] = g[P - 1], ++P;
dp[a[i].r] = max(dp[a[i].r], g[a[i].l - 1] + f[i]), g[P] = max(g[P - 1], dp[a[i].r]);
l = a[i].r;
}
printf("%d\n", g[a[n].r]);
rep(i, 1, a[n].r) dp[i] = f[i] = g[i] = 0;
}
return 0;
}
CF1399F Yet Another Segments Subset的更多相关文章
- Array and Segments (Easy version) CodeForces - 1108E1 (暴力枚举)
The only difference between easy and hard versions is a number of elements in the array. You are giv ...
- CodeForces - 1101G :(Zero XOR Subset)-less(线性基)
You are given an array a1,a2,…,an of integer numbers. Your task is to divide the array into the maxi ...
- Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】
传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...
- codeforces 1101G (Zero XOR Subset)-less 前缀异或+线性基
题目传送门 题意:给出一个序列,试将其划分为尽可能多的非空子段,满足每一个元素出现且仅出现在其中一个子段中,且在这些子段中任取若干子段,它们包含的所有数的异或和不能为0. 思路:先处理出前缀异或,这样 ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- [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 ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Largest Divisible Subset 最大可整除的子集合
Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...
- Greenplum记录(一):主体结构、master、segments节点、interconnect、performance monitor
结构:Client--master host--interconnect--segment host 每个节点都是单独的PG数据库,要获得最佳的性能需要对每个节点进行独立优化. master上不包含任 ...
随机推荐
- C++异常处理(try catch throw)完全攻略
程序运行时常会碰到一些异常情况,例如: 做除法的时候除数为 0: 用户输入年龄时输入了一个负数: 用 new 运算符动态分配空间时,空间不够导致无法分配: 访问数组元素时,下标越界:打开文件读取时,文 ...
- Spring第一个程序
目录 1.利用Maven导入jar包 2.编写一个实体类 3.编写Spring文件 4.测试 1.利用Maven导入jar包 <dependency> <groupId>org ...
- CVE-2021-45232 Apache APISIX 从未授权访问到RCE
00x1漏洞环境 Apache APISIX Dashboard 2.7 - 2.10 版本受到影响 通过git拉取在docker搭建环境 git clone https://github.com/a ...
- MATLAB m文件编译错误之错误使用slCharacterEncoding
错误提示: 错误使用 slCharacterEncoding (line 51) Close all block diagrams (using 'bdclose all') before tryin ...
- 关于java的Excel导入导出之easypoi
导入easypoi相关jar包,这里的easypoi-base的包也可以不倒入,因为easypoi-web中有依赖easypoi-base会自动导入的 <!-- https://mvnrepos ...
- java运算符1
一:算术运算符(+, -, *, /, ++, --, ) 1.+号 :可以做加法运算(加号两边为字符和数字).正数表示 字符串连接符:只要+号两边其中有一边有字符串,输出时加号就充当连接 ...
- python中的break 和continue的区别
break语句可以在循环过程中直接退出循环,而continue语句可以提前结束本轮循环 break的例子如图,当遇到的n为偶数时,直接退出循环,所以打印的结果只有1. continue例子如下图,当遇 ...
- 779. 第K个语法符号
<找规律> <递归> 题目描述 在第一行我们写上一个 0.接下来的每一行,将前一行中的0替换为01,1替换为10. 给定行数 N 和序数 K,返回第 N 行中第 K个字符.(K ...
- [网络编程] 自己构建一个cgi.FieldStorage()的对象
问题描述: 通常cgi.FieldStorage()返回一个类似于Python字典的对象. 在cgi框架中必须通过浏览器发送表单过来才能接受消息 那么我该怎么进行本地调试呢? 或者说在没有搭建好一整套 ...
- js字符串首字母大写的不同写法
写法一: let name = 'hello' name.charAt(0).toUpperCase() + name.slice(1) 写法二: let name = 'hello' name.sl ...