C. Add Zeros(cf982)

题意:给定长度为n的数组a,可以进行以下操作:选择一个位置i,并且ai=a的长度+1-i;在a的末尾添加i-1个零,多次执行操作后数组a的最大可能长度是多少

分析:ai=|a|-i+1,所以|a|=ai+i-1,用map记录每个点都需要多少长度才可以变,记得vis开大点不然放不下

代码:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e6+10;
ll a[N];
map<ll,bool>vis;
ll ans=0;ll n;
map<ll,vector<ll>>mp; 
void dfs(ll x){
	vis[x]=1;
	ans=max(ans,x);
	for(auto &xx:mp[x]){
		if(vis[x+xx-1]==0){
			dfs(x+xx-1);
		}
	}
}
void sol(){
	cin>>n;
	mp.clear();ans=0;
	vis.clear();
	for(int i=1;i<=n;i++){
	    cin>>a[i];
		if(i>1)mp[a[i]+i-1].push_back(i);	
	} 
	dfs(n);
	cout<<ans<<endl;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
    int t;cin>>t;
	while(t--)sol();
	return 0;
}
相关推荐
冷小鱼12 小时前
AI Agent 核心算法:任务规划(Planning)的深度技术解析
人工智能·算法·planning
杜子不疼.12 小时前
【C++ 在线五子棋对战】- 会话管理模块实现
开发语言·c++
退休倒计时12 小时前
【每日一题】LeetCode 78. 子集 TypeScript
算法·leetcode·typescript
有点。12 小时前
C++深度优先搜索(DFS)的概念(一)
开发语言·c++·深度优先
旖-旎12 小时前
《LeetCode 978 最长湍流子数组 || LeetCode 139 单词拆分》
c++·算法·leetcode·动态规划
aaPIXa62212 小时前
C++大型项目模块化拆分实战记录
开发语言·c++
cxr82812 小时前
第16章 跨域迁移与能力融合——从专家到通才
人工智能·算法·智能体·hermes
石山代码12 小时前
C++23 新特性在 CLion 中的实战体验
开发语言·c++·c++23
学究天人12 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(补充卷9)
人工智能·线性代数·算法·数学建模·动态规划·原型模式·抽象代数
Keven_1113 小时前
算法札记:图的存储方式
算法·图论