首页 > 其他分享 >CF958C3. Encryption (hard)

CF958C3. Encryption (hard)

时间:2023-06-21 11:45:46浏览次数:35  
标签:取模 ch int Encryption CF958C3 hard right 转移 105

谁说 \(n\le5\times 10^5\),\(k\le100\),\(p\le100\) 只能 \(O(nk)\)?我今天就要用 \(O(nk\log p)\) 过这个题!

定义 \(f_{i,j}\) 表示前 \(j\) 个数,分成 \(i\) 段的最小价值和,\(s_i\) 表示前缀和(对 \(p\) 取模),转移就是 \(f_{i,j}=\min\limits_{l=1}^{j-1}\left\{f_{i-1,l}+\left(s_j-s_l\right)\bmod p\right\}\)。朴素转移是 \(O(n^2k)\) 的。

但是我们发现,我们不需要枚举具体从哪个位置转移过来,只要知道那个位置的前缀和对 \(p\) 取模的值即可。更具体地,我们在转移的时候维护一个数组 \(g\),\(g_i\) 表示所有前缀和对 \(p\) 取模为 \(i\) 的转移中最小的,显然这个可以在转移的同时 \(O(1)\) 更新,复杂度就优化到了 \(O(nkp)\)。

这样肯定还是过不了的,但我们可以利用数据结构再次优化,用树状数组维护 \(g\) 数组:对于 \(\left(s_j-s_l\right)\bmod p\) 这一项,发现它在 \(s_l\le s_j\) 的时候等于 \(s_j-s_l\),反之等于 \(s_j-s_l+p\)。所以我们可以开两个树状数组,分别维护前/后缀 \(f_{i-1,l}-s_l\) 的最小值。时间复杂度 \(O(nk\log p)\)。

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int inf=1e9;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int n,k,p,f[105][500005],s[500005],c[105],d[105];
inline void addp(int x,int y){for(x++;x<=p;x+=x&-x)c[x]=min(c[x],y);}
inline void adds(int x,int y){for(x=p-x;x<=p;x+=x&-x)d[x]=min(d[x],y);}
inline int askp(int x){int res=inf;for(x++;x;x-=x&-x)res=min(res,c[x]);return res;}
inline int asks(int x){int res=inf;for(x=p-x;x;x-=x&-x)res=min(res,d[x]);return res;}
signed main(){
	n=read(),k=read(),p=read();
	for(int i=1;i<=n;++i)s[i]=(s[i-1]+read())%p,f[1][i]=s[i];
	for(int i=2;i<=k;++i){
		for(int j=0;j<p;++j)c[j+1]=d[p-j]=inf;
		for(int j=1;j<=n;++j)f[i][j]=min(askp(s[j]),asks(s[j]+1)+p)+s[j],addp(s[j],f[i-1][j]-s[j]),adds(s[j],f[i-1][j]-s[j]);
	}
	printf("%d\n",f[k][n]);
	return 0;
}

标签:取模,ch,int,Encryption,CF958C3,hard,right,转移,105
From: https://www.cnblogs.com/xx019/p/17495872.html

相关文章

  • Docker配置SpringBoot+ShardingJDBC+MybatisPlus项目实现分库分表与读写分离
    Docker配置SpringBoot+ShardingJDBC+MybatisPlus项目实现分库分表与读写分离 分类于 实战例子本文ShardingJDBC相关知识主要参考自ShardingJDBC官方文档,更多的用法建议到官网文档查看。前言传统的业务系统都是将数据集中存储至单一数据节点的解决方案,如今随着互联网数据......
  • 1494. 并行课程 II (Hard)
    问题描述1494.并行课程II(Hard)给你一个整数n表示某所大学里课程的数目,编号为1到n,数组relations中,relations[i]=[xᵢ,yᵢ]表示一个先修课的关系,也就是课程xᵢ必须在课程yᵢ之前上。同时你还有一个整数k。在一个学期中,你最多可以同时上k门课,前提是这......
  • 1494. Parallel Courses II (Hard)
    Description1494.ParallelCoursesII(Hard)Youaregivenanintegern,whichindicatesthattherearencourseslabeledfrom1ton.Youarealsogivenanarrayrelationswhererelations[i]=[prevCoursei,nextCoursei],representingaprerequisiterelat......
  • 552.Student Attendance Record II (Hard)
    Description552.StudentAttendanceRecordII(Hard)Anattendancerecordforastudentcanberepresentedasastringwhereeachcharactersignifieswhetherthestudentwasabsent,late,orpresentonthatday.Therecordonlycontainsthefollowingthre......
  • 1483. Kth Ancestor of a Tree Node (Hard)
    Description1483.KthAncestorofaTreeNode(Hard)Youaregivenatreewithnnodesnumberedfrom0ton-1intheformofaparentarrayparentwhereparent[i]istheparentofithnode.Therootofthetreeisnode0.Findthekthancestorofagive......
  • 智能合约HardHat框架环境的搭建
    1.首先创建一个npm项目PSC:\Users\lcds\blockchainprojects>mkdirhardhatcontractPSC:\Users\lcds\blockchainprojects>cd.\hardhatcontract\2.运行 npminit-y  初始化项目PSC:\Users\lcds\blockchainprojects\hardhatcontract>npminit-yWrotetoC:\......
  • [ABC162E] Sum of gcd of Tuples (Hard)
    题面翻译给定\(n,k\),求\[\sum^k_{a_1=1}\sum^k_{a_2=1}\sum^k_{a_3=1}\dots\sum^k_{a_n=1}gcd(a_1,a_2,a_3,\dots,a_n)\mod\1000000007\]制約$2\\leq\N\\leq\10^5$$1\\leq\K\\leq\10^5$。思路点拨我们看到这么多\(\gcd\)的式子,我们自然想到莫比乌斯反演......
  • Doosan Excavator Inspection Diagnostic Tool DDT SCR DPF G2 Scan DCU ECU DMS-5 Ha
    DoosanExcavatorInspectionDiagnosticToolDDTSCRDPFG2ScanDCUECUDMS-5Hardware+Software2022.09Softwaredownloadlink:https://mega.nz/file/Bk8X1QxA#g49TrmFsIljfHQpAIkQlG-VIWSgug8kLq3VffqAW00YHardware+SoftwareVersionDoosanDDTSCRDoosan......
  • 2681. 英雄的力量 (Hard)
    问题描述2681.英雄的力量(Hard)给你一个下标从0开始的整数数组nums,它表示英雄的能力值。如果我们选出一部分英雄,这组英雄的力量定义为:i₀,i₁,...iₖ表示这组英雄在数组中的下标。那么这组英雄的力量为max(nums[i₀],nums[i₁]...nums[iₖ])²*min(nums[i₀]......
  • 41.缺失的第一个正数 (Hard)
    问题描述41.缺失的第一个正数(Hard)给你一个未排序的整数数组nums,请你找出其中没有出现的最小的正整数。请你实现时间复杂度为O(n)并且只使用常数级别额外空间的解决方案。示例1:输入:nums=[1,2,0]输出:3示例2:输入:nums=[3,4,-1,1]输出:2示例3:输入:nums=[......