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上不包含任 ...
随机推荐
- Windows 11实现直播,VLC超简单实现捕获、串流、播放
上一篇文章说了搭建Nginx的rtmp服务.实现直播功能 期间发现一个更便捷的工具 VLC media play,官方下载:https://www.videolan.org 1.傻瓜式安装,略过 2. ...
- Unsupervised Domain Adaptation by Backpropagation
目录 概 主要内容 代码 Ganin Y. and Lempitsky V. Unsupervised Domain Adaptation by Backpropagation. ICML 2015. ...
- [Box] Robust Training and Initialization of Deep Neural Networks: An Adaptive Basis Viewpoint
目录 概 主要内容 LSGD Box 初始化 Box for Resnet 代码 Cyr E C, Gulian M, Patel R G, et al. Robust Training and In ...
- SpringBoot集成MyBatis-Plus自定义SQL
1.说明 本文介绍Spring Boot集成MyBatis-Plus框架后, 基于已经创建好的Spring Boot工程, 添加自定义的SQL实现复杂查询等操作. 自定义SQL主要有两种方式, 一种是 ...
- ActiveMQ基础教程(三):C#连接使用ActiveMQ消息队列
接上一篇:ActiveMQ基础教程(二):安装与配置(单机与集群) 安装部署好集群环境:192.168.209.133:61616,192.168.209.134:61616,192.168.209. ...
- GeoWave实践2:使用命令行将本地矢量数据注入hbase并发布至GeoServer
目录 步骤 补充 步骤 在GeoWave所在的节点打开两个会话窗口. 会话1 //运行GeoServer,默认端口8080 geowave gs run 会话2 //为GeoWave创建数据库Test ...
- CSS基础6之盒子模型1
盒子概述 以下是盒子模型的一个图形解释 一.内边距(填充) 属性有: (1) padding 设置所有内边距 (2) padding-top 设置上边距 (3) padding-left 设置左边 ...
- Oracle 查询NULL字段/空字符串
简单记录一下: 工作中需要查询某个字段值为空的数据, 最开始查询的时候按照以前的思路 : 1.where 字段名=NULL,点击F8,未查到结果: 2.where 字段名='',点击F8,未查到结果: ...
- js 关于replace() 的使用心得
1.前言 我想把一段话 let a = "抱歉,您当前的主治医生有紧急情况不得不下班,您的预约将由<br>医生:里斯<br>为您就诊,<br>诊室位置:门 ...
- vue3.0+vite+ts项目搭建--vite.config.ts配置(三)
vite.config.ts配置 配置路径处理模块 安装ts的类型声明文件 yarn add @types/node -D 通过配置alias来定义路径的别名 resolve: { alias: { ...