欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 1321B Journey Planning(思维)

发布时间:2024/4/11 编程问答 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 CodeForces - 1321B Journey Planning(思维) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目链接:点击查看

题目大意:给出一个长度为 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);}

     

    总结

    以上是生活随笔为你收集整理的CodeForces - 1321B Journey Planning(思维)的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。