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上不包含任 ...
随机推荐
- 深入理解Java虚拟机二:垃圾收集与内存分配
垃圾收集:垃圾收集要完成三件事,包括哪些内存需要回收,什么时候回收及如何回收. 1.需要回收的内存判定:没有引用指向原先分配给某个对象的内存时,则该内存是需要回收的垃圾 Java垃圾收集器在对内存进行 ...
- The Many Faces of Robustness: A Critical Analysis of Out-of-Distribution Generalization (DeepAugment)
目录 概 主要内容 ImageNet-R StreetView StoreFronts (SVSF) DeepFashion Remixed DeepAugment 实验结论 代码 Hendrycks ...
- Latex 入门教程
Latex 入门教程 学习途径:LaTex入门_哔哩哔哩_bilibili 运行环境:texlive2021.texstudio-4.1.2-win-qt6 1. 基本结构 整个 Latex 文件分为 ...
- docker学习:docker---kafka安装
下载zookeeper镜像与kafka镜像: docker pull wurstmeister/zookeeper docker pull wurstmeister/kafka 本地启动zookeep ...
- LDAP客户端安装
安装环境: 10.43.159.7 客户端 使用ldap客户端验证登陆: 用户为10.43.159.9服务端上面创建的ldap:zdh1234 1.安装LDAP client认证需要的pam包 yum ...
- 【操作系统】I/O多路复用 select poll epoll
@ 目录 I/O模式 I/O多路复用 select poll epoll 事件触发模式 I/O模式 阻塞I/O 非阻塞I/O I/O多路复用 信号驱动I/O 异步I/O I/O多路复用 I/O 多路复 ...
- Python原生数据结构增强模块collections
collections简介 python提供了4种基本的数据结构:list.tuple.dict.set.基本数据结构完全可以hold住所有的场景,但是在处理数据结构复杂的场景时,这4种数据结构有时会 ...
- 利用js 引用的方式 鼠标经过弹出效果
js引用的是此网站代码: https://www.cnblogs.com/jq-growup/p/15609469.html <!DOCTYPE html> <html lang=& ...
- centos7 安装zabbix3.0 安装zabbix4.0 yum安装zabbix 国内源安装zabbix 阿里云服务器安装zabbix
首先,此篇文章是有原因的. 刚开始也和大家一样来学习安装zabbix 奈何网上的教程和现实出现不一样的情况 在安装zabbix过程中,因为zabbix下载源是在国外,下载途中会出现终止下载的情况 tr ...
- spring boot -- 配置文件application.properties 换成 application.yml
1.前言 其实两种配置文件在spring boot 的作用一样,只是写法不同 ,yml 可以写的内容更少 ,以树结构 书写内容,看起来很清晰, 但是 如果 项目配置文件设置为 既有properties ...