虽然一开始就觉得从右下角左上角直接dp2次是不行的,后面还是这么写了WA了

两次最大的并不一定是最大的,这个虽然一眼就能看出,第一次可能会影响第二次让第二次太小。

这是原因。

5

4 32 1 18 41

47 38 7 43 43

48 23 39 40 23

26 39 33 5 36

31 29 7 26 47

这组数据是结果。

走完第一遍成

0 32 1 18 41

0 38 7 43 43

0 0 0 0 0

26 39 33 5 0

31 29 7 26 0

这样倒着走回去一定会经过0导致第二遍小很多。

正确走法

5

0 32 1 18 41

0 38 7 43 43

0 23 39 40 23

0 0 0 5 36

31 29 0 0 0

然后

5

0 0 1 18 41

0 0 7 43 43

0 0 0 0 0

0 0 0 5 0

31 29 0 0 0

这样和为508比两次取最大的482还大。

完全想不到怎么弄了。上一次求两次和最短路的走过了后就不能走,也想成两次跑最小,也是跪,结果应该用最小费用流。



这个也是没发现,不管怎么走一定每次确定步数后都会在同一条对角线上。

所以可以一条对角线一条一条的推。然而要同时确定两条路,走过去再走回来就等于走过去两条路,经过同一个点的时候只取一次。

dp[d][i][j]表示到第d条对角线时第一条路在i行,第二条路在j行最大的和能取多大,一路推到最后,这里可以用滚动数组dp内存就只有n*n了。

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 305;
const ll mod = 1e9 + 7;
const double eps = 1e-12;
int a[N][N];
int dp[2][N][N];
int main() {
int n;cin >> n;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++)
cin >> a[i][j];
memset(dp, -2, sizeof dp);
dp[1][1][1] = a[1][1];
int now = 1;
for (int d = 2;d < n + n;d++) {
now ^= 1;
for (int i = max(1,d-n+1);i <= min(n,d);i++)
for (int j = max(1,d-n+1);j <= min(n,d);j++) {
for (int x = i - 1;x <= i;x++) {
for (int y = j - 1;y <= j;y++) {
if (x >= max(1, d - n) && x <= min(n, d - 1) && y >= max(1,d - n) && y <= min(n, d - 1)) {
int val = a[i][d - i + 1] + a[j][d - j + 1];
if (i == j)val /= 2;
dp[now][i][j] = max(dp[now][i][j], dp[now ^ 1][x][y] + val);
}
}
}
}
for (int i = 0;i <= n;i++)
for (int j = 0;j <= n;j++)dp[now ^ 1][i][j] = -1000000000;
}
cout << dp[now][n][n];
return 0;
}

codeforces.com/problemset/problem/213/C的更多相关文章

  1. http://codeforces.com/problemset/problem/594/A

    A. Warrior and Archer time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. http://codeforces.com/problemset/problem/712/D

    D. Memory and Scores time limit per test 2 seconds memory limit per test 512 megabytes input standar ...

  3. http://codeforces.com/problemset/problem/847/E

    E. Packmen time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  4. http://codeforces.com/problemset/problem/545/D

    D. Queue time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  5. codeforces 340C Tourist Problem

    link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...

  6. codeforces B. Routine Problem 解题报告

    题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...

  7. Codeforces 527D Clique Problem

    http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...

  8. Codeforces 706C - Hard problem - [DP]

    题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...

  9. Codeforces 1096D - Easy Problem - [DP]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...

随机推荐

  1. ArrayList调用remove方法需要注意的地方

    ArrayList中有remove 方法和 removeAll方法, ArrayList中不仅继承了接口Collection中的remove方法,而且还扩展了remove方法. Collection中 ...

  2. n0_n1

    #include<stdio.h>int a[10];void quanpailie(int i){ if(i==10)  {for(i=0;i<10;i++)  {   print ...

  3. your project contains error(s),please fix them before running your application.错误总结

             Android开发中的问题总是多种多样,今天我来总结一个浪费了我一个晚上的错误T-T:your project contains error(s),please fix them b ...

  4. 获取表单的初始值,模拟placeholder属性

    input和textarea有一个默认属性defaultValue,即初始值. 即使在页面操作修改了input和textarea的内容,获取到的defaultValue依然是初始值.可通过该值模拟pl ...

  5. C#访问url地址并返回数据

    public partial class Form1 : Form { static bool isSelect = false; public Form1() { InitializeCompone ...

  6. Java学习-020-Properties 判断是否存在对应的 key 项

    在日常的脚本编写过程中,通常会判断配置文件中是否存在对应的配置项,以判断是否执行相应的业务逻辑. 小二上码...若有不足之处,敬请大神指正,不胜感激! 判断是否存在 key 项(配置项)的方法源码如下 ...

  7. background-image中url找不到路径,背景图像无法显示

    <div id="body_left_secend_1">                        <img src="~/Images/imag ...

  8. angularjs工程流程走不通的原因以及使用angularjs流程注意点

    1 入口index.html 在这个页面中要引入一些js,也就是说无论哪个模块下的js以及css都是在index.html下引入的,而在其他非index.html的html页面中,只有div模块代码, ...

  9. JS-面向对象-封装

    --参考文献: --http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_encapsulation.html --js面 ...

  10. html5引用公共头尾

    <embed type="text/html" src="head.html" />