http://codeforces.com/contest/677/problem/E

题意:有n*n矩形,每个格子有一个值(0、1、2、3),你可以在矩形里画一个十字(‘+’形或‘x’形),十字的四条边需等长。问十字覆盖的格子的值累乘最大是多少?

思路:

1、防止溢出,在比较大小更新答案时用加法替换乘法:a*b==log(a)+log(b);

2、首先,遍历每个点,对于每个点,对8个方向dfs,直到越界或值为0;求出每个点各个方向的深度后,第二遍遍历时可以得到十字的长度,然后算出若以该点为中心点它的最大贡献,更新答案。

关键数组:

dep[dir][i][j]:以i、j为中心点,向方向dir走,最深能走多远;

sum[dir][i][j]:以i、j为中心点,向方向dir走,走到最深时这一路的贡献和;

优化:

同一个方向的dfs数据是可以重复利用的,如4-3-2-1,第一次dfs算出了3-2-1的数据,对于4来说,如果可以走,只要加上3的数据就可以结束4的dfs了;

备注:ans初始化应该为-1,0会错;不优化会超时;

 import java.io.*;
import java.util.Arrays; public class a {
private static final int c = 1010; static int[][][] dep = new int[8][c][c];
static double[][][] sum = new double[8][c][c];
static final int[] dx = {1, 0, -1, 0, 1, 1, -1, -1};
static final int[] dy = {0, 1, 0, -1, 1, -1, -1, 1};
static void dfs(int d, int x, int y) {
if (dep[d][x][y] != -1) return;
int xx = x + dx[d], yy = y + dy[d];
if (Math.min(xx, yy) < 1 || Math.max(xx, yy) > n || a[xx][yy] == 0) {
dep[d][x][y] = 1;
sum[d][x][y] = lg[x][y];
return;
}
dfs(d, xx, yy);
dep[d][x][y] = dep[d][xx][yy] + 1;
sum[d][x][y] = sum[d][xx][yy] + lg[x][y];
} static int n;
static int[][] a = new int[c][c];
static double[][] lg = new double[c][c]; public static void main(String[] args) {
final int mod = (int) (1e9 + 7);
IO io = new IO();//自己写的类,没有贴出来
n = io.nextInt();
for (int i = 0; i < dep.length; i++)
for (int j = 0; j < dep[0].length; j++) Arrays.fill(dep[i][j], -1); int dis, rr = 0, cc = 0, res_dis = 0, res_s = 0;
double cur, ans = -1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) if ((a[i][j] = io.nextChar() - '0') != 0) lg[i][j] = Math.log(a[i][j]);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
for (int k = 0; k < 8; k++) if (dep[k][i][j] == -1 && a[i][j] != 0) dfs(k, i, j);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (a[i][j] != 0) for (int s = 0; s <= 1; s++) {
dis = c;
cur = lg[i][j];
for (int k = s * 4; k <= s * 4 + 3; k++) dis = Math.min(dis, dep[k][i][j]);
for (int k = s * 4; k <= s * 4 + 3; k++)
cur += sum[k][i][j] - lg[i][j] - sum[k][i + dis * dx[k]][j + dis * dy[k]];
if (cur > ans) {
ans = cur;
rr = i;
cc = j;
res_dis = dis;
res_s = s;
}
}
if (res_dis == 0) {
io.println(0);
return;
}
long res = a[rr][cc];
for (int i = 1; i <= res_dis - 1; i++)
for (int t = res_s * 4; t <= res_s * 4 + 3; t++) res = res * a[rr + i * dx[t]][cc + i * dy[t]] % mod;
io.println(res);
}
}

E. Vanya and Balloons Codeforces Round #355 (Div. 2)的更多相关文章

  1. D. Vanya and Treasure Codeforces Round #355 (Div. 2)

    http://codeforces.com/contest/677/problem/D 建颗新树,节点元素包含r.c.dis,第i层包含拥有编号为i的钥匙的所有节点.用i-1层更新i层,逐层更新到底层 ...

  2. Codeforces Round #355 (Div. 2) D. Vanya and Treasure 分治暴力

    D. Vanya and Treasure 题目连接: http://www.codeforces.com/contest/677/problem/D Description Vanya is in ...

  3. Codeforces Round #355 (Div. 2) C. Vanya and Label 水题

    C. Vanya and Label 题目连接: http://www.codeforces.com/contest/677/problem/C Description While walking d ...

  4. Codeforces Round #355 (Div. 2) B. Vanya and Food Processor 水题

    B. Vanya and Food Processor 题目连接: http://www.codeforces.com/contest/677/problem/B Description Vanya ...

  5. Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题

    A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...

  6. Codeforces Round #355 (Div. 2) D. Vanya and Treasure dp+分块

    题目链接: http://codeforces.com/contest/677/problem/D 题意: 让你求最短的从start->...->1->...->2->. ...

  7. Codeforces Round #355 (Div. 2)-B. Vanya and Food Processor,纯考思路~~

    B. Vanya and Food Processor time limit per test 1 second memory limit per test 256 megabytes input s ...

  8. Codeforces Round #355 (Div. 2) D. Vanya and Treasure

    题目大意: 给你一个n × m 的图,有p种宝箱, 每个点上有一个种类为a[ i ][ j ]的宝箱,a[ i ][ j ] 的宝箱里有 a[ i ][ j ] + 1的钥匙,第一种宝箱是没有锁的, ...

  9. Codeforces Round #355 (Div. 2)C - Vanya and Label

    啊啊啊啊啊啊啊,真的是智障了... 这种题目,没有必要纠结来源.只要知道它的结果的导致直接原因?反正这句话就我听的懂吧... ">>"/"&" ...

随机推荐

  1. Linux(一)—— Unix&Linux 历史

    Linux(一)-- Unix&Linux 历史 Unix =Unix内核+Unix实用工具 Unix Unix 的诞生 Unix的历史可以追溯到20世纪60年代中期,当时麻省理工学院,AT& ...

  2. sql 视图学习

    视图语句 在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表. 视图包含行和列,就像一个真实的表.视图中的字段就是来自一个或多个数据库中的真实的表中的字段. 您可以向视图添加 SQL 函数. ...

  3. Ubuntu 16.04 安装Google 浏览器

    Ubuntu安装好后,自带Firefox浏览器,有时我们需要再安装几个浏览器,那么Google Chrome,就是首选, 安装如下: 下载浏览器安装包, 下载链接:https://dl.google. ...

  4. LinkedList与Queue

    https://blog.csdn.net/u013087513/article/details/52218725

  5. openwrt 里LUA程序怎么获取POST数据?

    https://www.zhihu.com/question/31579325 作者:齐葛链接:https://www.zhihu.com/question/31579325/answer/28342 ...

  6. Codeforces Goodbye 2018

    Goodbye 2018 可能是我太菜考试的时候出不了$E$ 可能是我太菜考试的时候调不出$F$ 所以转化为手速场之后手速还上不去.jpg A 模拟题意... #include <cstdio& ...

  7. [原创]一种专门用于前后端分离的web服务器(JerryServer)

    如果你还不了解现在的前后端分离,推荐阅读淘宝前端团队的前后端分离的思考与实践 1.问题 随着现在整个软件开发行业的发展,在开发模式上逐渐由以前的一个人完成服务端和前端web页面,演变为前端和后端逐渐分 ...

  8. 记一次CPU飙升BUG

    图文地址:https://mp.weixin.qq.com/s?__biz=Mzg3NjEzODQ4NQ==&mid=2247483690&idx=1&sn=7c926f400 ...

  9. input type=file的几个属性

    <input type='file' /> inputDom.onchange=function (e){ e.currentTarget.files  是只有一个对象的数组 var ob ...

  10. Vue echarts

    方式一.直接引入echarts 先 npm 安装 echarts npm install echarts --save // main.js import myCharts from './comm/ ...