码迷,mamicode.com
首页 > 其他好文 > 详细

[CF430D]Working out(DP)

时间:2016-08-24 01:09:16      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://codeforces.com/problemset/problem/430/D

题意:两个人在操场上分别从左上角和左下角到右下角和右上角,左上角的人只能向右向下,左下角的人只能向右向上。他们必须在某一时刻见面一次,操场上每个点有一个值,问如何选取交点使得他们的值总和最大,他们交点的值不计。

先在四个角到对角线的角做dp,求出最长的路径,再根据两个人的行走情况枚举所有可能的交点,并统计最值。

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <fstream>
 24 #include <cassert>
 25 #include <cstdio>
 26 #include <bitset>
 27 #include <vector>
 28 #include <deque>
 29 #include <queue>
 30 #include <stack>
 31 #include <ctime>
 32 #include <set>
 33 #include <map>
 34 #include <cmath>
 35 using namespace std;
 36 #define fr first
 37 #define sc second
 38 #define cl clear
 39 #define BUG puts("here!!!")
 40 #define W(a) while(a--)
 41 #define pb(a) push_back(a)
 42 #define Rint(a) scanf("%d", &a)
 43 #define Rll(a) scanf("%I64d", &a)
 44 #define Rs(a) scanf("%s", a)
 45 #define Cin(a) cin >> a
 46 #define FRead() freopen("in", "r", stdin)
 47 #define FWrite() freopen("out", "w", stdout)
 48 #define Rep(i, len) for(int i = 0; i < (len); i++)
 49 #define For(i, a, len) for(int i = (a); i < (len); i++)
 50 #define Cls(a) memset((a), 0, sizeof(a))
 51 #define Clr(a, x) memset((a), (x), sizeof(a))
 52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
 53 #define lrt rt << 1
 54 #define rrt rt << 1 | 1
 55 #define pi 3.14159265359
 56 #define RT return
 57 #define lowbit(x) x & (-x)
 58 #define onecnt(x) __builtin_popcount(x)
 59 typedef long long LL;
 60 typedef long double LD;
 61 typedef unsigned long long ULL;
 62 typedef pair<int, int> pii;
 63 typedef pair<string, int> psi;
 64 typedef pair<LL, LL> pll;
 65 typedef map<string, int> msi;
 66 typedef vector<int> vi;
 67 typedef vector<LL> vl;
 68 typedef vector<vl> vvl;
 69 typedef vector<bool> vb;
 70 
 71 const int maxn = 1010;
 72 int dp[5][maxn][maxn];
 73 int a[maxn][maxn];
 74 int n, m;
 75 
 76 int main() {
 77     // FRead();
 78     scanf("%d %d", &n, &m);
 79     for (int i = 1; i <= n; ++i)
 80         for (int j = 1; j <= m; ++j)
 81             scanf("%d", &a[i][j]);
 82     for (int i = 1; i <= n; ++i)
 83         for (int j = 1; j <= m; ++j)
 84             dp[1][i][j] = a[i][j] + max(dp[1][i-1][j], dp[1][i][j-1]);
 85     for (int i = n; i >= 1; --i)
 86         for (int j = m; j >= 1; --j)
 87             dp[4][i][j] = a[i][j] + max(dp[4][i][j+1], dp[4][i+1][j]);
 88     for (int i = n; i >= 1; --i)
 89         for (int j = 1; j <= m; ++j)
 90             dp[3][i][j] = a[i][j] + max(dp[3][i][j-1], dp[3][i+1][j]);
 91     for (int i = 1; i <= n; ++i)
 92         for (int j = m; j >= 1; --j)
 93             dp[2][i][j] = a[i][j] + max(dp[2][i-1][j], dp[2][i][j+1]);
 94     int ans = 0;
 95     for (int i = 2; i < n; ++i)
 96         for (int j = 2; j < m; ++j) {
 97             ans = max(ans, dp[1][i][j-1] + dp[4][i][j+1] + dp[3][i+1][j] + dp[2][i-1][j]);
 98             ans = max(ans, dp[1][i-1][j] + dp[4][i+1][j] + dp[3][i][j-1] + dp[2][i][j+1]);
 99         }
100     printf("%d\n", ans);
101     return 0;
102 }

 

[CF430D]Working out(DP)

标签:

原文地址:http://www.cnblogs.com/vincentX/p/5801255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!