[ARC150F] Constant Sum Subsequence
Problem Statement
We have a sequence of positive integers of length $N^2$, $A=(A_1,\ A_2,\ \dots,\ A_{N^2})$, and a positive integer $S$. For this sequence of positive integers, $A_i=A_{i+N}$ holds for positive integers $i\ (1\leq i \leq N^2-N)$, and only $A_1,\ A_2,\ \dots,\ A_N$ are given as the input.
Find the minimum integer $L$ such that every sequence $B$ of positive integers totaling $S$ is a (not necessarily contiguous) subsequence of the sequence $(A_1,\ A_2,\ \dots,\ A_L)$ of positive integers.
It can be shown that at least one such $L$ exists under the Constraints of this problem.
Constraints
- $1 \leq N \leq 1.5 \times 10^6$
- $1 \leq S \leq \min(N,2 \times 10^5)$
- $1 \leq A_i \leq S$
- For every positive integer $x\ (1\leq x \leq S)$, $(A_1,\ A_2,\ \dots,\ A_N)$ contains at least one occurrence of $x$.
- All values in the input are integers.
有一个明显的 dp,定义 \(dp_i\) 为包含所有何为 \(i\) 的串的最小前缀。枚举一个 \(j\),那么 \(dp_i=\max_{j<i}nx_{dp_j,i}\) 当中 \(nx_{x,y}\) 表示 \(x\) 后面出现的第一个 \(y\)。 dp 明显是存在单调性的。
考虑数据结构优化,但是没有什么特别适合的数据结构使用。神奇地,考虑CDQ。
如果现在的分支区间为 \([l,r]\),中心为 \(md\),那么计算所有 \([l,md]\) 的 DP 值对 \([md+1,r]\) 的 dp 值的贡献。枚举 \(i-j\),然后发现只有最接近 \(dp_{md}\) 的哪个 \(i-j\) 才是有用的,设他在 \(p\)。只有 $[ls_p,p) $ 这段区间内的数才可以得到贡献,把他对应到 dp 上面就行了。按照 \(p\) 从大到小排序后,用区间覆盖线段树维护 dp 值,然后更新 dp 值即可。
复杂度:\(O(Slog^2n+nlogn)\)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int S=2e5+5,N=1.5e6+5;
int n,s,a[N];
LL dp[S],tr[S<<2],tag[S<<2];
vector<int>g[S];
struct node{
int l,r,p;
bool operator<(const node&n)const{
return r<n.r;
}
}st[S];
void pushdown(int o,int l,int r)
{
if(~tag[o])
{
tag[o<<1]=tag[o];
tag[o<<1|1]=tag[o];
tr[o<<1]=tag[o];
tr[o<<1|1]=tag[o];
tag[o]=-1;
}
}
void upd(int o,int l,int r,int x,int y,LL z)
{
if(x>y)
return;
if(x<=l&&r<=y)
return tag[o]=tr[o]=z,void();
pushdown(o,l,r);
int md=l+r>>1;
if(md>=x)
upd(o<<1,l,md,x,y,z);
if(md<y)
upd(o<<1|1,md+1,r,x,y,z);
}
LL qry(int o,int l,int r,int x)
{
if(l==r)
return tr[o];
pushdown(o,l,r);
int md=l+r>>1;
if(md>=x)
return qry(o<<1,l,md,x);
return qry(o<<1|1,md+1,r,x);
}
void solve(int l,int r)
{
if(l==r)
return;
int md=l+r>>1,tp=0;
solve(l,md);
upd(1,1,s,l,r,0);
int tmx=(dp[md]-1)%n+1,p=(dp[md]-1)/n;
for(int i=1;i<=r-l;i++)
{
vector<int>::iterator it=upper_bound(g[i].begin(),g[i].end(),tmx);
if(it==g[i].end())
st[++tp]=(node){*(--it),g[i][0]+n-1,i};
else if(it==g[i].begin())
st[++tp]=(node){g[i][g[i].size()-1]-n,g[i][0]-1,i};
else
st[++tp]=(node){*(it-1),(*it)-1,i};
}
sort(st+1,st+tp+1);
for(int i=1;i<=tp;i++)
{
int kl=lower_bound(dp+l,dp+md+1,1LL*p*n+st[i].l)-dp,kr=upper_bound(dp+l,dp+md+1,1LL*p*n+st[i].r)-dp-1;
upd(1,1,s,kl+st[i].p,min(kr+st[i].p,s),st[i].r+1LL*p*n+1);
}
for(int i=md+1;i<=r;i++)
dp[i]=max(dp[i],qry(1,1,s,i));
solve(md+1,r);
}
int main()
{
memset(tag,-1,sizeof(tag));
scanf("%d%d",&n,&s);
for(int i=1;i<=n;i++)
{
scanf("%d",a+i),g[a[i]].push_back(i);
if(!dp[a[i]])
dp[a[i]]=i;
}
solve(1,s);
printf("%lld\n",dp[s]);
}
[ARC150F] Constant Sum Subsequence的更多相关文章
- Codeforces 797B - Odd sum
B. Odd sum 题目链接:http://codeforces.com/problemset/problem/797/B time limit per test 1 second memory l ...
- Odd sum (对本菜鸡来说是个极坑题)
https://codeforces.com/problemset/problem/797/B time limit per test 1 second memory limit per test 2 ...
- [经典] 最X(长 | 大和 | 大积)Y(子序列 | 子字符串)
Note: 子序列,可以不连续:子字符串,必须连续. 以下题目按在我看看来的难度从易到难排列: 最大和子序列(Maximum sum subsequence) 这道题纯属娱乐...应该不会有人出这种题 ...
- Petrozavodsk Summer-2016. Ural FU Dandelion Contest
A. Square Function 留坑. B. Guess by Remainder 询问$lcm(1,2,3,...,n)-1$即可一步猜出数. 计算$lcm$采用分治FFT即可,时间复杂度$O ...
- codeforces 797B
B. Odd sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Educational Codeforces Round 19 B
Description You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to f ...
- 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)
1, N2N_2N2, ..., NKN_KNK }. A continuous subsequence is defined to be { NiN_iNi, Ni+1N_{i ...
- 【BZOJ-3638&3272&3267&3502】k-Maximum Subsequence Sum 费用流构图 + 线段树手动增广
3638: Cf172 k-Maximum Subsequence Sum Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 174 Solved: 9 ...
- Maximum Subsequence Sum(接上篇)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
随机推荐
- SpringBoot+Mybatis-Plus+Mysql的保姆级搭建
本文通过简单的示例代码和说明,让读者能够了解Mybatis-Plus+Mysql的简单使用 必须说明的是,本文有部分内容是为了后续的微服务写的,所以如果只想用Mybatis-Plus的话,直接使用ba ...
- Python+Flask设置接口开机自启动
Windows系统适用 创建一个批处理文件(例如 start_flask_api.bat),内容如下: @echo off cd /d C:\path\to\your\flask\app //你要启动 ...
- vue3 甘特图(二):甘特图时间轴切换
vue3 甘特图(二):gantt时间轴切换 1.固定时间轴缩放级别 gantt.config.scale_unit = "month"; //时间轴单位 gantt.config ...
- GORM自定义Gorm.Model实现自动添加时间戳
废话不说直接开始 官网(http://gorm.io)有给出一套默认的gorm.Model模型,定义如下 package gorm import "time" // Model b ...
- Kruskal重构树 学习笔记
Kruskal 重构树 最大生成树将部分内容倒置即可 回顾:Kruskal 基本信息 求解最小生成树 时间复杂度:\(O(m \log m)\) 更适合稀疏图 算法思想 按照边权从小到大排序 依次枚举 ...
- Spring Boot虚拟线程与Webflux在JWT验证和MySQL查询上的性能比较
早上看到一篇关于Spring Boot虚拟线程和Webflux性能对比的文章,觉得还不错.内容较长,我就不翻译了,抓重点给大家介绍一下这篇文章的核心内容,方便大家快速阅读. 测试场景 作者采用了一个尽 ...
- 2023-09-27:用go语言,在一个 n x n 的国际象棋棋盘上,一个骑士从单元格 (row, column) 开始, 并尝试进行 k 次移动。行和列是 从 0 开始 的,所以左上单元格是 (0
2023-09-27:用go语言,在一个 n x n 的国际象棋棋盘上,一个骑士从单元格 (row, column) 开始, 并尝试进行 k 次移动.行和列是 从 0 开始 的,所以左上单元格是 (0 ...
- 爬虫系列——Beautifulsoup4
文章目录 一 介绍 二 基本使用 三 遍历文档树 四 搜索文档树 五 修改文档树 六 总结 一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能 ...
- Oracle中的substr()函数和INSTR()函数和mysql中substring_index函数字符截取函数用法:计算BOM系数用量拼接字符串*计算值方法
最近一直在研究计算产品BOM的成本系数,将拼接的元件用量拼接后拆分计算是个问题,后来受到大佬在mysql中截取字符串的启发在oracle中以substr和instr实现了 1.以下是我在mysql中 ...
- 每天5分钟复习OpenStack(三)
每天5分钟复习OpenStack(三) 为什么要拉起kvm 虚拟机要熟悉这些操作? 作为一个运维工程师,将来有大量的时间是在制作镜像,镜像的制作就是在kvm虚拟化环境拉起kvm 管理的虚拟机的过程,安 ...