题目链接:点击查看
题目大意:给出一个长度为 n 的数列,规定本题中的上升子序列必须满足两个条件:
- a[ j ] < a[ i ]
- a[ i ] - a[ j ] = i - j
问累加和最大的上升子序列为多少
题目分析:比赛的时候一直以为是dp,最后才发现只是一个简单思维,首先对于上面的第二个条件移项,可以得到上升子序列的第二个条件就变成了a[ i ] - i = a[ j ] - j,也就是可以构成上升子序列的一系列序列,其值与位置的差都是相等的,那么只需要O(n)维护一下答案就好了,注意,因为位置的范围是1~2e5,而值的范围是1~4e5,所以可能会出现-2e5这样的坐标,为了方便处理,我们可以配合map进行统计
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int N=2e5+100;
map<int,LL>mp;
int main()
{
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);
int n;
LL ans=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int num;
scanf("%d",&num);
mp[num-i]+=num;
ans=max(ans,mp[num-i]);
}
printf("%lld\n",ans);
}