Codeforces 1016C Vasya And The Mushrooms
題目連結:http://codeforces.com/contest/1016/problem/C
題意:從左上角格子開始走,每次走一個臨近的格子,速率為1,走過的格子取走所有的蘑菇,每個格子上的蘑菇有一個增長速率,問獲得的最大的增長速率是多少
分析:dp。除了樣例中給的兩種走法,還有可能是兩種走法的結合。預處理出來每次折線走法的每一個格子獲得的速率值,繞圈走法左邊的列相當於它右邊的一列加上一個字尾和(因為走過一格後面的格子相當於時間多了1,加上增長速率和即可),為了保證獲得的速率值最大,只能是先折線走再繞圈走,繞圈走可能是從上到下或者從下到上,類似處理即可,注意long long 。
程式碼:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
typedef long long ll;
ll a[maxn],b[maxn];
ll S1[maxn],S2[maxn],S3[maxn],S4[maxn],SS[maxn];
int n;
void init1()//走折線,從上往下走
{
for(int i = 1; i <= n; i++)
{
if(i % 2)
{
S1[i] = a[i] * (2 * i - 2);
S1[i] += S1[i - 1];
S2[i] = b[i] * (2 * i - 1);
S2[i] += S1[i];
}
else
{
S2[i] = b[i] * (2 * i - 2);
S2[i] += S2[i - 1];
S1[i] = a[i] * (2 * i - 1);
S1[i] += S2[i];
}
}
}
void init2()
{
ll ans = 0;
for(int i = n; i >= 1; i--)
{
ans += a[i] + b[i];
SS[n - i + 1] = ans;
}
}
void init3()//前面的由後面的轉移過來,每次增加一個字尾和
{
for(int i = n,l = 0; i >= 1; i--,l++)//從上到下
{
S3[i] = S3[i + 1] + SS[l] + b[i] * (2 * l + 1);
}
for(int i = n,l = 0; i >= 1; i--,l++)//從下到上
{
S4[i] = S4[i + 1] + SS[l] + a[i] * (2 * l + 1);
}
}
int main()
{
cin>>n;
for(int i = 1; i <= n; i++)
{
cin>>a[i];
}
for(int i = 1; i <= n; i++)
{
cin>>b[i];
}
init1();
init2();
init3();
ll ans = 0;
ans = S3[1];
for(int i = 1; i <= n; i++)
{
if(i % 2 == 0)
{
ans = max(ans,S1[i] + S3[i + 1] + (i * 2) * SS[n - i]);
}
else
{
ans = max(ans,S2[i] + S4[i + 1] + (i * 2) * SS[n - i]);
}
}
cout<<ans<<endl;
return 0;
}
相關文章
- codeforces Vasya and Digital RootGit
- Codeforces Round #206 (Div. 2) A. Vasya and Digital RootGit
- CF1945F Kirill and MushroomsOOM
- Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 二分+前字尾預處理
- HDU 4422 The Little Girl who Picks Mushrooms(簡單題)OOM
- CF1107F Vasya and Endless Credits
- Codeforces
- codeforces Mafia
- codeforces Winner
- codeforces Educational Codeforces Round 33 (Rated for Div. 2)
- 【CodeForces訓練記錄】Codeforces Global Round 27
- codeforces Educational Codeforces Round 33 (Rated for Div. 2)B
- codeforces Photo of The Sky
- Codeforces - Jzzhu and Numbers
- codeforces #442 B
- Codeforces Sereja and Array
- Codeforces Round #174
- Codeforces Round #170
- Codeforces Round 955
- Codeforces 專區
- 9.11 codeforces
- codeforces 11/10
- 【CodeForces訓練記錄】Codeforces Round 981 (Div. 3)
- 【CodeForces訓練記錄】Codeforces Round 982 (Div. 2)
- 【CodeForces訓練記錄】Codeforces Round 984 (Div. 3)
- 【CodeForces訓練記錄】Codeforces Round 986 (Div. 2)
- Codeforces Global Round 13
- codeforces_B. Barnicle
- CodeForces 1307
- CodeForces - 1363B
- Educational Codeforces Round 32
- Codeforces Beta Round #32
- Codeforces Beta Round #23
- Codeforces Round #180
- Codeforces Global Round 26
- Codeforces Global Round 27
- CodeForces - 765F
- Codeforces 983 A-E