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;
}
相关推荐
阿Y加油吧8 分钟前
算法实战笔记:LeetCode 169 多数元素 & 75 颜色分类
笔记·算法·leetcode
t***54415 分钟前
如何在Dev-C++中选择Clang编译器
开发语言·c++
不要秃头的小孩17 分钟前
力扣刷题——509. 斐波那契数
python·算法·leetcode·动态规划
汉克老师1 小时前
GESP2023年9月认证C++三级( 第一部分选择题(9-15))
c++·gesp三级·gesp3级
We་ct1 小时前
LeetCode 120. 三角形最小路径和:动态规划详解
前端·javascript·算法·leetcode·typescript·动态规划
py有趣1 小时前
力扣热门100题之和为K的子数组
数据结构·算法·leetcode
hipolymers2 小时前
C语言怎么样?难学吗?
c语言·数据结构·学习·算法·编程
workflower3 小时前
机器人应用-楼宇室内巡逻
大数据·人工智能·算法·microsoft·机器人·动态规划·享元模式
ZPC82103 小时前
fanuc 机器人通过PR寄存器实现轨迹控制
人工智能·算法·计算机视觉·机器人