CF 1016C Vasya And The Mushrooms (DP)

本文详细解析了CodeForces 1016C蘑菇收集问题,介绍了一个森林中蘑菇生长的特殊场景,通过算法优化实现了在限定时间内收集最大重量蘑菇的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

codeforces 1016c

题目:

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.

题意:两排格子, 每个格子会有一个权值, 从左上的格子出发,时间点为零, 每次只能走相邻的格子(上下左右),每次穿越格子花费时间1点,到达每个格子时会得到权值  时间 * 格子权值, 要求走完所有格子且不能走已经走过的格子, 求所得权值的最大值。

题解:首先观察可知一共有n个终点,所以自然想到遍历一遍复杂度应该为O(n),但是要求和,如果不加以处理必定会上升到O(n^2)的复杂度,考虑以下:sum123表示从左到右走到头所摘蘑菇总数,sun321表示从右到左所摘蘑菇总数,sum111表示这一段初始蘑菇总和。我们观察到,不管什么时候开始走sum123+sum321的情况(即一路走到头再从上面回来,中间不转弯),都与原始求的sum123与sum321差一个常数*sum111,因为可把复杂度变为O(n)。

AC代码:

#include<cstdio>
#include<algorithm>
using namespace std;
long long sum123[2][300001], sum321[2][300001], sum111[2][300001];
int n;
int a[2][300001];
int main()
{
    int i, j;
    long long sum = 0, res = 0, nres;
    scanf("%d", &n);
    for(i = 0;i < 2;i++)
            for(j = 0;j < n;j++)
                scanf("%d", &a[i][j]);
    for(i = 0;i < 2;i++){
        for(j = n - 1;j >= 0;j--){
            sum123[i][j] = sum123[i][j + 1] + j * 1LL * a[i][j];//从i,j一直走到右边端点所摘磨菇数总和
            sum321[i][j] = sum321[i][j + 1] + (n - j) * 1LL * a[i][j];//从右端点走到i,j所摘磨菇数总和
            sum111[i][j] = sum111[i][j + 1] + a[i][j];//初始磨菇数总和
        }
    }
    for(i = 0, j = 0;j < n;++j, i ^= 1){//学会位运算,k^=1表示:k为偶数k-=1,k为奇数k+=1
        nres = sum; //用sum维护左端一直拐弯的蘑菇总和
        nres += sum123[i][j] +j * 1LL * sum111[i][j];//补上一个常数(j+1)*sum111的蘑菇数量
        nres += sum321[i^1][j] + (j + n - 1) * 1LL * sum111[i ^ 1][j];//补上一个常数(j+n)*sum111的蘑菇数量
        res = max(res, nres);//更新总和
        sum += a[i][j] * 1LL * (j + j);//更新维护的和
        sum += a[i ^ 1][j] * 1LL * (j + j + 1);
    }
    printf("%I64d\n", res);
    return 0;
}

 

 

### 关于 Vasya 和多重集的编程竞赛算法题目解决方案 #### 题目描述 给定一个多重集合 `s`,目标是将其分割成两个新的多重集合 `a` 和 `b` (其中一个可以为空),使得这两个新集合中的“好数”的数量相等。“好数”定义为在一个特定多集中恰好只出现一次的数字。 为了实现这一目标,需要考虑如何有效地统计并分配这些元素到不同的子集中去[^1]。 #### 解决思路 一种有效的解决方法是从输入数据的特点出发思考。如果某个数值在整个原始集合中出现了偶数次,则该值可以在不影响最终结果的情况下被平均分入两个子集中;而对于那些仅出现奇数次数的情况,则必须小心处理以确保能够达成平衡条件——即让尽可能多的不同类型的单例项分别进入各自的目标组内[^2]。 具体来说: - 对于任何频率大于等于两次(无论是奇还是偶)的数据点而言,总是能通过适当划分来满足上述要求; - 当遇到频度为一的情形时,就需要额外注意了:因为这直接影响着能否成功创建具有相同数目唯一成员的新分区。 因此,在实际编码过程中应该优先处理那些重复率较高的项目,并记录下所有独一无二实例的位置以便后续操作使用。 #### Python 实现代码示例 下面是一个基于此逻辑编写的Python函数,用于求解这个问题: ```python from collections import Counter def can_split_equally(s): count = Counter(s) # 统计各元素出现次数 singletons = sum(1 for v in count.values() if v == 1) return singletons % 2 == 0 # 测试用例 test_cases = [ [1, 2, 2, 3], # True 可以分成 {1} 和 {2, 2, 3} [1, 2, 3, 4, 5], # False 单独存在的数字有五个无法平分 ] for case in test_cases: print(f"Input: {case}, Can Split Equally? :{can_split_equally(case)}") ``` 这个程序首先利用 `collections.Counter` 来计算每个整数在列表里边出现过的总次数。接着它会遍历所有的键值对,累积起所有只出现过一次(也就是所谓的 “好数” 或者说是单一实例)的数量。最后一步就是判断这样的特殊案例是不是构成了一个偶数序列长度 —— 如果是的话就意味着存在至少一组可行解;反之则不存在这样的一分为二方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值