codeforces 1321 B. Journey Planning(贪心)
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>ck−1ck>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+1−ci=bci+1−bcici+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 (1≤n≤2⋅1051≤n≤2⋅105) — the number of cities in Berland.
The second line contains nn integers b1b1, b2b2, ..., bnbn (1≤bi≤4⋅1051≤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.
Examples
input
Copy
6 10 7 1 9 10 15
output
Copy
26
input
Copy
1 400000
output
Copy
400000
input
Copy
7 8 9 26 11 12 29 14
output
Copy
55
Note
The optimal journey plan in the first example is c=[2,4,5]c=[2,4,5].
The optimal journey plan in the second example is c=[1]c=[1].
The optimal journey plan in the third example is c=[3,6]c=[3,6].
题意:
给出 n 个旅游地点的美丽度 b ,设相邻两次旅游地点前后为第 i 个旅游地和第 j 个旅游地,需满足 j > i && j - i == b[ j ] - b[ i ]
思路:
符合条件即美丽度差与下标差相等,把所有美丽度减去下标,统计相同的数据对应的美丽度和,取最大值
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int a[N], b[N];
int main()
{
int n;
while(~scanf("%d", &n))
{
for(int i = 1; i <= n; ++i)
{
scanf("%d", &a[i]);
b[i] = a[i] - i;
}
map<int, ll>mp;
map<int, ll>::iterator k;
for(int i = 1; i <= n; ++i)
{
mp[b[i]] += (ll)a[i];
}
ll ans = 0;
for(k = mp.begin(); k != mp.end(); ++k)
{
ans = max(ans, k -> second);
}
cout<<ans<<'\n';
mp.clear();
}
return 0;
}
上一篇: CodeForces - 854C Planning(贪心 + 优先队列)
下一篇: 新思路
推荐阅读
-
CodeForces - 854C Planning(贪心 + 优先队列)
-
codeforces 1321 B. Journey Planning(贪心)
-
Codeforces 1321 C. Remove Adjacent(贪心枚举)
-
Codeforces Round #681 B. Saving the City(贪心)
-
Codeforces Round #277.5 (Div. 2)-B. BerSU Ball (贪心)_html/css_WEB-ITnose
-
Codeforces Round #277.5 (Div. 2)-B. BerSU Ball (贪心)_html/css_WEB-ITnose
-
Codeforces Beta Round #3 B. Lorry(贪心)