zoukankan      html  css  js  c++  java
  • C. Vasya And The Mushrooms

    C. Vasya And The Mushrooms
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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
    input
    Copy
    3
    1 2 3
    6 5 4
    output
    Copy
    70
    input
    Copy
    3
    1 1000 10000
    10 100 100000
    output
    Copy
    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.

    比赛的时候没写出来,思路不清晰就没写出来,

    赛后补题感觉这种题就是要思路清晰的时候写.

    由于每个网格都必须经过一次,所以从左上角开始的路线只有两类:“↓→↑→↓→↑→” 折返以及在某一次往右时停止折返,一直往右直到最后一格,然后返回,一般的情况如下(也可能在第二行一直往右,到最后从第一行返回)

    先做好预处理,就是先遍历所有从头开始,dfs就是实现这个东西.

    接着预处理后缀和,之后就是从后面往前退,当然要判断一下奇偶.

     1 #include <bits/stdc++.h>
     2 #define ll long long int
     3 #define inf 0x3f3f3f3f
     4 #define N 400000
     5 using namespace std;
     6 int dis[3][2]={1,0,-1,0,0,1};
     7 ll n;
     8 ll a[2][N],sum[N];
     9 int c[2][N];
    10 ll val[2][N],xn[2][N];
    11 bool vis[2][N];
    12 bool is(int x,int y){ return x>=0&&x<2&&y>=0&&y<n; }
    13 void dfs(int x,int y,ll cc){
    14     vis[x][y] = true;
    15     c[x][y] = cc;
    16     for(int i=0;i<3;i++){
    17         int xx = x + dis[i][0];
    18         int yy = y + dis[i][1];
    19         if(is(xx,yy)&&!vis[xx][yy]){
    20             xn[xx][yy] = xn[x][y] + cc*a[x][y];
    21             dfs(xx,yy,cc+1);
    22         }
    23     }
    24 }
    25 
    26 int main(){
    27     scanf("%d",&n);
    28     for(int i=0;i<2;i++){
    29         for(int j=0; j<n;j++){
    30             scanf("%lld",&a[i][j]);
    31             sum[j] += a[i][j];
    32         }
    33     }
    34     for(int i=n-2;i>=0;--i)
    35         sum[i] += sum[i+1];//后缀和
    36     dfs(0,0,0);
    37     int up,down;
    38     if(n&1){
    39         up = n-1;
    40         ll cc = c[0][up];
    41         val[0][up] = cc*a[0][up] + (cc + 1)*a[1][up];
    42         down = n-2;
    43         cc = c[1][down];
    44         val[1][down] = cc*a[1][down] + (cc+1)*a[1][down+1] +(cc+2)*a[0][down+1] + (cc+3)*a[0][down];
    45     }else{
    46         up = n-2;
    47         ll cc = c[0][up];
    48         val[0][up] = cc*a[0][up] + (cc+1)*a[0][up+1] + (cc+2)*a[1][up+1] + (cc+3)*a[1][up];
    49         down = n-1;
    50         cc = c[1][down];
    51         val[1][down] = cc*a[1][down] + (cc+1)*a[0][down];
    52     }
    53 
    54     for(int i = up; i>=2;i=i-2){
    55         ll cc = c[0][i-2];
    56         val[0][i-2] = val[0][i] - sum[i]*2;
    57         val[0][i-2] += cc*a[0][i-2] + (cc+1)*a[0][i-1];
    58         cc += (n-i)*2 + 2;
    59         val[0][i-2] += cc*a[1][i-1] + (cc+1)*a[1][i-2];
    60     }
    61 
    62     for(int i = down; i>=2; i=i-2){
    63         ll cc = c[1][i-2];
    64         val[1][i-2] = val[1][i] - sum[i]*2;
    65         val[1][i-2] += cc*a[1][i-2] + (cc+1)*a[1][i-1];
    66         cc +=(n-i)*2 + 2;
    67         val[1][i-2] += cc*a[0][i-1] + (cc+1)*a[0][i-2];
    68      }
    69 
    70      ll ans = 0;
    71      for(int i=0;i<2;++i){
    72          for(int j=0;j<n;++j){
    73              if((i+j)%2==0){
    74                  ans = max(ans,val[i][j]+xn[i][j]);
    75              }
    76          }
    77      }
    78      printf("%lld
    ",ans);
    79     return 0;
    80 }
  • 相关阅读:
    java处理图片压缩、裁剪
    List按对象元素自定义排序
    List和数组汉字简单排序(转)
    欢迎访问我的个人博客,部分文章迁移
    Java资源免费分享,每日一更新,找到你心仪的吧
    今年的校招,Java好拿offer吗?
    【拥抱大厂系列】几个面试官常问的垃圾回收器,下次面试就拿这篇文章怼回去!
    2019,我的这一年,在校研究生做到年入20W,另送我的读者2000元现金红包
    深入理解Java虚拟机-如何利用VisualVM对高并发项目进行性能分析
    深入理解Java虚拟机-常用vm参数分析
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/9427516.html
Copyright © 2011-2022 走看看