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;
}
相关推荐
载数而行5201 小时前
QT的五类布局
c++·qt·学习
故事和你912 小时前
sdut-程序设计基础Ⅰ-实验五一维数组(8-13)
开发语言·数据结构·c++·算法·蓝桥杯·图论·类和对象
载数而行5202 小时前
QT的QString类
c++·qt·学习
像污秽一样2 小时前
算法与设计与分析-习题4.2
算法·排序算法·深度优先·dfs·bfs
Storynone3 小时前
【Day20】LeetCode:39. 组合总和,40. 组合总和II,131. 分割回文串
python·算法·leetcode
bu_shuo3 小时前
Visual C++2010学习版(全国计算机等级二级考试版)安装记录
c++·cpp·visual c++·计算机二级
明明如月学长3 小时前
AI 更新太快学不过来?我用OpenClaw打造专属AI学习工作流
算法
黎阳之光3 小时前
【黎阳之光:以无线专网与视频孪生,赋能智慧广电与数字中国】
算法·安全·智慧城市·数字孪生
刀法如飞4 小时前
Agentic AI时代,程序员必备的算法思想指南
人工智能·算法·agent
刀法如飞4 小时前
Agentic AI时代程序员必备算法思想详解(附实战案例)
算法·ai编程·编程开发·agentic