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. SQLServer之创建数据库快照

    创建数据库快照注意事项 语法:set transaction isolation level snapshot; 指定事务中任何语句读取的数据都将是在事务开始时便存在的数据的事务上一致的版本. 事务只 ...

  2. C# Base64方式的编码与解码

    编码与解码方法: ///编码 public static string EncodeBase64(string code_type, string code) { string encode = &q ...

  3. [十二省联考2019]D1T2字符串问题

    嘟嘟嘟 省选Day1真是重大失误,T2连暴力都没时间写. 上周五重新答了遍Day1,竟然搞了187分吼吼吼吼. T2按40分写的暴力,结果竟然得了60分. 稍微说一下暴力吧:预处理哈希,对于一组支配关 ...

  4. docker 不能访问外网

    如果之前docker能访问外网,现在不能访问, 同时宿主机可以访问外网,那就重启docker.

  5. WIFI KILL神器

    https://anky.cc/esp8266-deauther-wifi-jammer/ https://github.com/spacehuhn http://tieba.baidu.com/p/ ...

  6. VUE 简单属性操作

    在main.js内配置路由及相应模板 import Vue from 'vue' import App from './App' // 引入router路由 import Router from 'v ...

  7. 01构建第一个SpringBoot工程

    第一篇:构建第一个SpringBoot工程 文章指导 学习笔记 学习代码 创建项目 创建工程:Idea-> new Project ->Spring Initializr ->填写g ...

  8. iOS 打包.framework(包括第三方、图片、xib、plist文件)详细步骤及需要注意的地方

    https://www.cnblogs.com/yk123/p/9340268.html // 加载自定义名称为Resources.bundle中对应images文件夹中的图片// 思路:从mainb ...

  9. webmagic保存数据

    使用多线程:

  10. hashChange & url change & QRCode & canvas to image

    hashChange & url change & QRCode & canvas to image "use strict"; /** * * @auth ...