题目:http://codeforces.com/contest/1016/problem/C

一定是先蛇形走在回形走,所以预处理、暴力即可;

自己一开始写了一个,总是WA,又看了看TJ写法;

模仿一下,还是WA,发现中间的一些地方 int 相乘要加 (ll)!

然后把我原来那个改一下就A了...

20分钟写,60分钟调...

不过模仿的那个写法比我原来的快多了(虽然做法一样)。

模仿:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const maxn=3e5+;
int n,g[][maxn];
ll s[][maxn],f[][maxn][],ans;
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)scanf("%d",&g[][i]);
for(int i=;i<n;i++)scanf("%d",&g[][i]);
for(int j=;j<=;j++)
for(int i=n-;i>=;i--)
{
f[j][i][]=f[j][i+][]+(ll)(n-i-)*g[j][i];//<-
f[j][i][]=f[j][i+][]+(ll)i*g[j][i];//-> //走过来一定在 i 时间或以后
s[j][i]=s[j][i+]+g[j][i];
}
ll w=,add=;
for(int x=,y=;x<n;x++,y^=)
{
add=x*s[y][x]+f[y][x][]+(x+n)*s[!y][x]+f[!y][x][];
ans=max(ans,w+add);
w+=(ll)g[y][x]**x+(ll)g[!y][x]*(*x+);
}
printf("%I64d\n",ans);
return ;
}

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int const maxn=3e5+;
int n,g[][maxn];
ll s[][maxn],f[][maxn][],ans;
void dfs(int x,int y,ll w)
{
if(x==n)return;
ll t=*x,tt=t+n-x;
ans=max(ans,w+t*s[y][x+]+f[y][x+][]+tt*s[!y][x+]+f[!y][x+][]);
dfs(x+,y^,w+(ll)(t+)*(g[][x+]+g[][x+])+g[!y][x+]);
}
int main()
{
cin>>n;
for(int i=;i<=n;i++)cin>>g[][i];
for(int i=;i<=n;i++)cin>>g[][i];
for(int j=;j<=;j++)
{
for(int i=n,k=;i;i--,k++)
{
f[j][i][]=f[j][i+][]+(ll)k*g[j][i];//<-
s[j][i]=s[j][i+]+g[j][i];
}
for(int i=n;i;i--)
{
f[j][i][]=f[j][i+][]+s[j][i];//->
}
}
dfs(,,);
cout<<ans-s[][]-s[][];
return ;
}

CF 1016 C —— 思路的更多相关文章

  1. vijosP1016 北京2008的挂钟

    vijosP1016 北京2008的挂钟 题目链接:https://vijos.org/p/1016 [思路] Dfs. 对操作搜索更加优秀,所以采用搜索每一个操作的使用次数,因为操作数为4则相当于没 ...

  2. LeetCode:17. Letter Combinations of a Phone Number(Medium)

    1. 原题链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 2. 题目要求 给定一 ...

  3. leetcode教程系列——Binary Tree

    tree是一种常用的数据结构用来模拟真实物理世界里树的层级结构.每个tree有一个根(root)节点和指向其他节点的叶子(leaf)节点.从graph的角度看,tree也可以看作是有N个节点和N-1个 ...

  4. CF 949D Curfew——贪心(思路!!!)

    题目:http://codeforces.com/contest/949/problem/D 有二分答案的思路. 如果二分了一个答案,首先可知越靠中间的应该大约越容易满足,因为方便把别的房间的人聚集过 ...

  5. CF Gym 100463B Music Mess (思路)

    好题,当时想了半个小时,我往图论方面去想了,把出现过的字符串当场点,然后相互连边,那么就构成了一个三角形,一个大于三个点的连通分量里有以下结论:度为二的点可能是track,度为大于二的点一定不是tra ...

  6. CF 1088(A , B , C , D)——思路

    http://codeforces.com/contest/1088 A:Ehab and another construction problem 输出 2 和 n(偶数的话)或者 2 和 n-1( ...

  7. CF 949C Data Center Maintenance——思路+SCC

    题目:http://codeforces.com/contest/949/problem/C 可以想到可能是每组c有连边的可能. 但别直接给c1.c2连边,那样之后会变得很不好做. 可以把一些限制放在 ...

  8. CF 1036B Diagonal Walking v.2——思路

    题目:http://codeforces.com/contest/1036/problem/B 比赛时只能想出不合法的情况还有走到终点附近的方式. 设n<m,不合法就是m<k.走到终点方式 ...

  9. CF Gym 100187A Potion of Immortality (思路,最坏情况的最小损失)

    根据兔子试药情况可以缩小范围,如果死了,不在试过的药里面,如果活着,在试过的药里. 最糟的情况: 两个原则 1.能确定魔药所在的范围的尽量大,2.死得兔子尽量多. 如果当前不知道情况的药n为k的二倍以 ...

随机推荐

  1. MongoDB 启动和关闭

    重启命令 service mongodb restart 启动命令 mongod -f /data/tools/mongodb/config/config.conf 必须要带配置文件才能启动 关闭命令 ...

  2. 「 HDU P3555 」 Bomb

    # 题目大意 给出 $\text{T}$ 个数,求 $[1,n]$ 中含 ‘49’ 的数的个数. # 解题思路 求出不含 '49' 的数的个数,用总数减去就是答案. 数位 $DP$,用记忆化来做. 设 ...

  3. 自定义属性Attribute的运用

    有时候需要一个枚举类,能够承载更多的信息,于是可以利用attribute这个特性. 首先编写自己业务需求类 [AttributeUsage(AttributeTargets.Field)] publi ...

  4. openpyxl操作excel文件

    https://blog.csdn.net/hunter_wyh/article/details/78498323

  5. graph.h

    #ifndef _GRAPH_#define _GRAPH_#include<stdio.h>#include<stdlib.h>#include<string.h> ...

  6. PAT 1129 Recommendation System

    Recommendation system predicts the preference that a user would give to an item. Now you are asked t ...

  7. [poj3537]Crosses and Crosses_博弈论

    Crosses and Crosses poj-3537 题目大意:给定一个1*n的网格,每次往格子内填一个$\times$,连续的三个即可获胜. 注释:$1\le n\le 2000$. 想法:我们 ...

  8. 洛谷—— P2330 [SCOI2005]繁忙的都市

    P2330 [SCOI2005]繁忙的都市 题目描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路 ...

  9. Java的23种设计模式(转)

    设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计模式于 ...

  10. Node: Updating npm's bundled node gyp

    Linux, Mac OS X, Solaris, etc. Unix is easy. Just run the following command. Use sudo if necessary. ...