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;
}
相关推荐
我想走路带风14 小时前
各自缓冲及实现
c++
青 春 记 忆14 小时前
LeetCode 53. 最大子数组和|Python 解法详解
python·算法·leetcode
豆瓣鸡14 小时前
算法日记 - Day10
算法
戴西软件14 小时前
戴西CAxWorks.VPG车辆工程仿真软件技术解析(上)——安全仿真体系的自动化构建
运维·网络·数据库·人工智能·算法·安全·自动化
zander25815 小时前
4. 寻找两个正序数组的中位数:用分割点代替合并
数据结构·算法
Kisorge15 小时前
【电机控制器】 基于STSPIN32G4的FOC控制
stm32·嵌入式硬件·算法
阿米亚波15 小时前
【C/C++包管理器】vcpkg(by microsoft)
c语言·c++·git·vscode·microsoft·github·vcpkg
Elivs15 小时前
RMSNorm函数
人工智能·算法·机器学习
tudousisi22215 小时前
P4447 [AHOI2018初中组] 分组 题解复盘
算法
SNAKEpc1213815 小时前
OpenGL(十一)- 变换管线
c语言·c++·算法·矩阵·图形渲染