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;
}
相关推荐
漫随流水33 分钟前
leetcode算法(111.二叉树的最小深度)
数据结构·算法·leetcode·二叉树
fpcc35 分钟前
跟我学C++中级篇——Linux中文件和链接及重定向
linux·c++
Fcy6482 小时前
C++ set&&map的模拟实现
开发语言·c++·stl
じ☆冷颜〃8 小时前
黎曼几何驱动的算法与系统设计:理论、实践与跨领域应用
笔记·python·深度学习·网络协议·算法·机器学习
数据大魔方8 小时前
【期货量化实战】日内动量策略:顺势而为的短线交易法(Python源码)
开发语言·数据库·python·mysql·算法·github·程序员创富
POLITE38 小时前
Leetcode 23. 合并 K 个升序链表 (Day 12)
算法·leetcode·链表
fpcc9 小时前
C++编程实践——链式调用的实践
c++
楚来客9 小时前
AI基础概念之八:Transformer算法通俗解析
人工智能·算法·transformer
Echo_NGC22379 小时前
【神经视频编解码NVC】传统神经视频编解码完全指南:从零读懂 AI 视频压缩的基石
人工智能·深度学习·算法·机器学习·视频编解码
会员果汁9 小时前
leetcode-动态规划-买卖股票
算法·leetcode·动态规划