[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, ...
随机推荐
- 角度新奇!第一次看到这样使用MyBatis的,看得我一愣一愣的。
你好呀,我是歪歪. 这期给大家分享一个读者给我分享的一个关于 MyBatis 的"编程小技巧",说真的,这骚操作,直接把我看得一愣一愣的. 我更情愿叫它:坑你没商量之埋雷大法. D ...
- 《Kali渗透基础》12. 无线渗透(二)
@ 目录 1:无线协议栈 1.1:ifconfig 1.2:iwconfig 1.3:iw 1.4:iwlist 2:无线网卡配置 2.1:查看无线网卡 2.2:查看信道频率 2.3:扫描附近 AP ...
- CEMS大学生综合测评管理系统
功能介绍 登录 首页 修改密码 提交申请 提交列表 数据可视化 审核列表 前端 components结构 搭建Vue项目 Vue3快速上手: https://cn.vuejs.org/guid ...
- 局域网内文件分享的简单方式:python - http.server
在局域网条件下,利用Python自带的HTTP服务功能提供文件共享服务是相对比较简单便捷的方式之一. 一.现实需求及前提条件 1. 文件的服务端(文件分享者)与接收端(文件接收者)在一个局域网,接收端 ...
- 前端远程调试方案 Chii 的使用经验分享
前端远程调试方案 Chii 的使用经验分享 Chii 是与 weinre 一样的远程调试工具 ,主要是将 web inspector 替换为最新的 chrome devtools frontend 监 ...
- 聊聊基于Alink库的主成分分析(PCA)
概述 主成分分析(Principal Component Analysis,PCA)是一种常用的数据降维和特征提取技术,用于将高维数据转换为低维的特征空间.其目标是通过线性变换将原始特征转化为一组新的 ...
- Mysql数据库、表设计规范指南
结合网上资料与项目实际情况,总结下列MYSQL数据库设计规范. 一.MYSQL数据库设计规范1.数据库命名规范采用26个英文字母(区分大小写)和0-9的自然数(经常不需要)加上下划线'_'组成,禁止使 ...
- 【RocketMQ】RocketMQ存储结构设计
CommitLog 生产者向Broker发送的消息,会以顺序写的方式,写入CommitLog文件,CommitLog文件的根目录由配置参数storePathRootDir决定,默认每一个CommitL ...
- HCTF 2023 wp
HCTF 2023 wp 一.Misc 1.玩原神玩的 分析:附件为一张图片 观察最后一行,明显有flag的格式 搜索得知是 对照得flag为:hctf{yuanlainiyewanyuanshenh ...
- C#/.NET/.NET Core优秀项目和框架精选(2023年10月更新,项目分类已整理完成欢迎大家踊跃提交PR一起完善让优秀的项目和框架不被埋没)
前言 帮助开发者发现功能强大.性能优越.创新前沿.简单易用的C#/.NET/.NET Core优秀项目和框架,无论你是寻找灵感.学习新技术.改进代码质量,还是想拓展自己的技术视野,都能为你提供有价值的 ...