CodeForces - 1016C Vasya And The Mushrooms
好久没有体会这种A题的快感了23333
一开始看错了,以为权值是从1开始的,不过这样不要紧,最后把算的答案减去两行数的和就是正确的答案了。
然后发现位于一个角上的时候,我们其实只有两种选择,一种是先一直走这一行走到头再返回来走,这样就走完了;另一种是走到这一列的另一行上然后再往右走一列。
第一种可以直接算,第二种dp一下就好啦,两种取一下最优。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=300005; inline int read(){
int x=0; char ch=getchar();
for(;!isdigit(ch);ch=getchar());
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return x;
} int n,a[2][N],now;
ll f[2][N],qz[2][N],zq[2][N],fq[2][N]; inline ll getzq(int l,int r){ return zq[now][r]-zq[now][l-1];}
inline ll getfq(int l,int r){ return fq[now][l]-fq[now][r+1];}
inline ll getqz(int l,int r){ return qz[now][r]-qz[now][l-1];} int main(){
n=read();
for(int o=0;o<2;o++){
for(int i=1;i<=n;i++) a[o][i]=read(),qz[o][i]=qz[o][i-1]+(ll)a[o][i];
for(int i=1;i<=n;i++) zq[o][i]=zq[o][i-1]+a[o][i]*(ll)i;
for(int i=n;i;i--) fq[o][i]=fq[o][i+1]+a[o][i]*(ll)(n-i+1);
} for(int i=n;i;i--)
for(int o=0;o<2;o++){
now=o,f[o][i]=getzq(i,n)-getqz(i,n)*(ll)(i-1);
now^=1,f[o][i]+=getfq(i,n)+getqz(i,n)*(ll)(n-i+1); ll tot=a[o][i]+2ll*a[o^1][i]+f[o^1][i+1];
now=0,tot+=2ll*getqz(i+1,n),now=1,tot+=2ll*getqz(i+1,n); f[o][i]=max(f[o][i],tot);
} now=0,f[0][1]-=getqz(1,n),now=1,f[0][1]-=getqz(1,n); printf("%I64d\n",f[0][1]);
return 0;
}
CodeForces - 1016C Vasya And The Mushrooms的更多相关文章
- codeforces 1016C - Vasya And The Mushrooms 【构造 + 思维】
题目链接:戳这里 题意:从(1,1)出发,一遍把格子走完,每个格子只能走一次.问怎么走总和最大. 解题思路:画图可知,总共就3种走法的混合. dw: 样例1的走法 up: 样例1反过来的走法 lp: ...
- codeforces C. Vasya And The Mushrooms (思维+模拟)
题意:给定一个2*n的矩形方格,每个格子有一个权值,从(0,0)开始出发,要求遍历完整个网格(不能重复走一个格子),求最大权值和,(权值和是按照step*w累加,step步数从0开始). 转载: 题解 ...
- CodeForces - 837E - Vasya's Function | Educational Codeforces Round 26
/* CodeForces - 837E - Vasya's Function [ 数论 ] | Educational Codeforces Round 26 题意: f(a, 0) = 0; f( ...
- Vasya And The Mushrooms CodeForces - 1016C (前缀和模拟)
大意: 给定2*n的矩阵, 每个格子有权值, 走到一个格子的贡献为之前走的步数*权值, 每个格子只能走一次, 求走完所有格子最大贡献. 沙茶模拟打了一个小时总算打出来了 #include <io ...
- 【Educational Codeforces Round 48 (Rated for Div. 2) C】 Vasya And The Mushrooms
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然在没有一直往右走然后走到头再往上走一格再往左走到头之前. 肯定是一直在蛇形走位.. 这个蛇形走位的答案贡献可以预处理出来.很容易 ...
- Codeforces 837E. Vasya's Function
http://codeforces.com/problemset/problem/837/E 题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)) ...
- Codeforces 837E Vasya's Function - 数论
Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = ...
- Codeforces 493C - Vasya and Basketball
C. Vasya and Basketball 题目链接:http://codeforces.com/problemset/problem/493/C time limit per test 2 se ...
- Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈
Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...
随机推荐
- Docker 配置国内镜像拉取中心,Configure docker to use faster registries in China.
Networking in China is really bad when it comes to using some cloud based tools like docker, it's us ...
- 【leetcode 简单】第二十三题 二叉树的最大深度
给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...
- 简易版jquery
最近写了一个简易版的jquery github地址:https://github.com/jiangzhenfei/Easy-Jquery 完成的方法: 1.$('#id') 2.extend扩展 ...
- yii2-widget-fileinput英文文档翻译
源地址:http://plugins.krajee.com/file-input 该插件是为bootstrap开发的增强版h5文件上传插件,具有多文件预览,多文件选择等功能.该插件提供了基于boots ...
- 残差网络(Residual Network)
一.背景 1)梯度消失问题 我们发现很深的网络层,由于参数初始化一般更靠近0,这样在训练的过程中更新浅层网络的参数时,很容易随着网络的深入而导致梯度消失,浅层的参数无法更新. 可以看到,假设现在需要更 ...
- tomcat和weblogic的区别
Tomcat是Apache基金会提供的Servlet容器,它支持JSP, Servlet和JDBC等J2EE关键技术,所以用户可以用Tomcat开发基于数据库,Servlet和JSP页面的Web应用, ...
- cin循环输入控制问题
之前写一个简单的输入节点值自动生成链表的测试程序,发现cin的输入控制好像在VC++6.0和VS2010中不一样,特此记录. 现在有以下代码: vector<int> ivec; int ...
- MySQL指定使用某个索引查询语句
查询语句查询emp_no,所以先查询emp_no的索引使用primary select emp_no,salary from salaries use index(s_f_t) where emp_n ...
- ftp,nfs和samba的区别
先从名字上进行理解: 1. FTP(文件传输协议) 2. NFS(网络文件系统) 3. samba 即smb(服务信息块)协议 1 其中FTP 是TCP/IP协议栈所提供的一种子协议,该子协议具体可以 ...
- linux中使用vim编译C++程序
Vi三种模式详解 命令行模式 (command mode/一般模式) 任何时候,不管用户处于何种模式,只要按一下“ESC”键,即可使Vi进入命令行模式:我们在shell环境(提示符为$)下输入启动Vi ...