题目链接
C. Vasya And The Mushrooms
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Vasya's house is situated in a forest, and there is a mushroom glade near it. The glade consists of two rows, each of which can be divided into n consecutive cells. For each cell Vasya knows how fast the mushrooms grow in this cell (more formally, how many grams of mushrooms grow in this cell each minute). Vasya spends exactly one minute to move to some adjacent cell. Vasya cannot leave the glade. Two cells are considered adjacent if they share a common side. When Vasya enters some cell, he instantly collects all the mushrooms growing there.
Vasya begins his journey in the left upper cell. Every minute Vasya must move to some adjacent cell, he cannot wait for the mushrooms to grow. He wants to visit all the cells exactly once and maximize the total weight of the collected mushrooms. Initially, all mushrooms have a weight of 0. Note that Vasya doesn't need to return to the starting cell.
Help Vasya! Calculate the maximum total weight of mushrooms he can collect.
Input
The first line contains the number n (1 ≤ n ≤ 3·105) — the length of the glade.
The second line contains n numbers a1, a2, ..., an (1 ≤ ai ≤ 106) — the growth rate of mushrooms in the first row of the glade.
The third line contains n numbers b1, b2, ..., bn (1 ≤ bi ≤ 106) is the growth rate of mushrooms in the second row of the glade.
Output
Output one number — the maximum total weight of mushrooms that Vasya can collect by choosing the optimal route. Pay attention that Vasya must visit every cell of the glade exactly once.
Examples
inputCopy
3
1 2 3
6 5 4
outputCopy
70
inputCopy
3
1 1000 10000
10 100 100000
outputCopy
543210
Note
In the first test case, the optimal route is as follows:
Thus, the collected weight of mushrooms will be 0·1 + 1·2 + 2·3 + 3·4 + 4·5 + 5·6 = 70.
In the second test case, the optimal route is as follows:
Thus, the collected weight of mushrooms will be 0·1 + 1·10 + 2·100 + 3·1000 + 4·10000 + 5·100000 = 543210.
题意:
给一个2*n的矩阵,每一个点有一个权值,从左上角出发,每走一步需要一个单位时间,时间t=0开始,要求每个格子必须走且仅走一次,使得 (\(\sum\)格子权值\(\times\)到达该格子时间) 最大。
题解:
由于要求每个格子必须走且仅走一次,可以发现,所有的合法路线都有这样的特征(分2个阶段):1.首先折返走“↓→↑→↓→↑→” 2.然后从某个格子开始一直往右走到最后一格,然后返回。大致形状如下:
因此只要预处理后枚举转折点即可。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
#define dbg(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<endl;
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const int inf=0x3fffffff;
const ll mod=1000000007;
const int maxn=3e5+100;
ll a[3][maxn];
ll sum321[3][maxn],sum123[3][maxn],sum[3][maxn];
int main()
{
int n;
scanf("%d",&n);
rep(i,1,3) rep(j,1,n+1) scanf("%lld",&a[i][j]);
rep(i,1,3)
{
rep(j,1,n+1)
{
sum123[i][j]=sum123[i][j-1]+1ll*(j-1)*a[i][j];
sum321[i][n-j+1]=sum321[i][n-j+2]+1ll*(j-1)*a[i][n-j+1];
sum[i][j]=sum[i][j-1]+a[i][j];
}
}
ll res=0;
ll cnt=0;
rep(i,1,n+1)
{
if(i&1)
{
res=max(res,cnt+sum123[1][n]-sum123[1][i-1]+(i-1)*(sum[1][n]-sum[1][i-1])+sum321[2][i]+(n+i-1)*(sum[2][n]-sum[2][i-1]));
cnt+=1ll*(2*i-2)*a[1][i]+1ll*(2*i-1)*a[2][i];
}
else
{
res=max(res,cnt+sum123[2][n]-sum123[2][i-1]+(i-1)*(sum[2][n]-sum[2][i-1])+sum321[1][i]+(n+i-1)*(sum[1][n]-sum[1][i-1]));
cnt+=1ll*(2*i-1)*a[1][i]+1ll*(2*i-2)*a[2][i];
}
}
printf("%lld\n",res);
return 0;
}