B. Journey Planning

Posted xuanmaiboy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了B. Journey Planning相关的知识,希望对你有一定的参考价值。

Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round)

B. Journey Planning
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Tanya wants to go on a journey across the cities of Berland. There are nn cities situated along the main railroad line of Berland, and these cities are numbered from 11 to nn.

Tanya plans her journey as follows. First of all, she will choose some city c1c1 to start her journey. She will visit it, and after that go to some other city c2>c1c2>c1, then to some other city c3>c2c3>c2, and so on, until she chooses to end her journey in some city ck>ck1ck>ck−1. So, the sequence of visited cities [c1,c2,,ck][c1,c2,…,ck] should be strictly increasing.

There are some additional constraints on the sequence of cities Tanya visits. Each city ii has a beauty value bibi associated with it. If there is only one city in Tanya‘s journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities cici and ci+1ci+1, the condition ci+1ci=bci+1bcici+1−ci=bci+1−bci must hold.

For example, if n=8n=8 and b=[3,4,4,6,6,7,8,9]b=[3,4,4,6,6,7,8,9], there are several three possible ways to plan a journey:

  • c=[1,2,4]c=[1,2,4];
  • c=[3,5,6,8]c=[3,5,6,8];
  • c=[7]c=[7] (a journey consisting of one city is also valid).

There are some additional ways to plan a journey that are not listed above.

Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?

Input

The first line contains one integer nn (1n21051≤n≤2⋅105) — the number of cities in Berland.

The second line contains nn integers b1b1, b2b2, ..., bnbn (1bi41051≤bi≤4⋅105), where bibi is the beauty value of the ii-th city.

Output

Print one integer — the maximum beauty of a journey Tanya can choose.

样例输入

7
8 9 26 11 12 29 14

输出

55

题意:就是给你一个下标为1~n的b数组,b[i]代表第i个城市的美丽值,让你从编号为1~n的城市里选一个序列使总的美丽值最大(全部城市的美丽值加起来),并且选的城市编号有先后要求编号只能递增,而且美丽值和城市编号要满足,b[i]-b[j]=i-j (i,j为城市编号,i>j)。

题解:

我的第一想法就是直接模拟,我以为给个标记数组就不会超时,但是你便利还是要一个一个试,时间复杂度还是要O(n^2),我就看啊看,题目只有一个式子:b[i]-b[j]=i-j (i,j为城市编号,i>j)。

这个式子我怎么搞呢?i,j怎么找?一个一个便利超时!

我变换一下:

b[i]-i=b[j]-j (i>j)

这样等式左右两边就没有直接的关系了,因为两边里面的数都是某一项的,不用和其他项扯上关系,那么时间复杂度O(n)是可以确定下来了!

变换后的等式是什么意思呢?就是每个“城市的美丽值和编号的差值”相同的城市我们都可以并入一个序列,然后比较每个序列的美丽值,取最大的即可!这里我们使用map s记录,s[i]表示差值为i的序列总的美丽值

代码如下:

需要注意的是map的查找复杂度为logn,所以总的时间复杂度其实为O(nlogn)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
typedef long long int ll;
const int mod=1e9+7;
ll b[200005];
int n;
ll ans=0ll;
int book[200005];
/*void dfs(int x)
{
    ll sum=b[x];
    int last=x;
    book[x]=1;
    ans=max(ans,sum);
    for(int i=x+1;i<=n;i++){
        if(b[i]-b[last]==i-last)
        {
            sum+=b[i];
            ans=max(ans,sum);
            last=i;
            book[i]=1;
        }
    }
    return; 
}*/
map<ll,ll> s;
int main(void)
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%lld",&b[i]);
    }
    for(int i=1;i<=n;i++){
        s[b[i]-i]+=b[i];
        ans=max(ans,s[b[i]-i]);
    }
    printf("%lld",ans);
    //printf("%lld
",s[-1]);
    return 0;
}

 

以上是关于B. Journey Planning的主要内容,如果未能解决你的问题,请参考以下文章

[Journey with golang] 0. Planning for the New year and new journey

Codeforces - 1321B - Journey Planning(思维)

[Codeforces 839C] Journey

POJ1935 Journey(树形dp)

CodeForces 721B Journey (DP)

Codeforces Round #374 (Div. 2) C. Journey