E. Vanya and Balloons Codeforces Round #355 (Div. 2)
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)的更多相关文章
- D. Vanya and Treasure Codeforces Round #355 (Div. 2)
http://codeforces.com/contest/677/problem/D 建颗新树,节点元素包含r.c.dis,第i层包含拥有编号为i的钥匙的所有节点.用i-1层更新i层,逐层更新到底层 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure dp+分块
题目链接: http://codeforces.com/contest/677/problem/D 题意: 让你求最短的从start->...->1->...->2->. ...
- 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 ...
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure
题目大意: 给你一个n × m 的图,有p种宝箱, 每个点上有一个种类为a[ i ][ j ]的宝箱,a[ i ][ j ] 的宝箱里有 a[ i ][ j ] + 1的钥匙,第一种宝箱是没有锁的, ...
- Codeforces Round #355 (Div. 2)C - Vanya and Label
啊啊啊啊啊啊啊,真的是智障了... 这种题目,没有必要纠结来源.只要知道它的结果的导致直接原因?反正这句话就我听的懂吧... ">>"/"&" ...
随机推荐
- C语言面试程序阅读整理
一.数组和指针 1.数组和指针的存储 写出下面的输出结果: char str1[] = "abc"; char str2[] = "abc"; const ch ...
- JAVA EE获取浏览器和操作系统信息
一.原理说明: 1. 浏览器访问服务端时,Http请求头上会带上客户端一些信息,可通过"user-agent"获取. //java获取方法如下,其他语言也有自己获取方法 Stri ...
- 如何查看linux中文件打开情况
前言 我们都知道,在linux下,“一切皆文件”,因此有时候查看文件的打开情况,就显得格外重要,而这里有一个命令能够在这件事上很好的帮助我们-它就是lsof. linux下有哪些文件 在介绍lsof命 ...
- Navicat Premium 12.1.16.0安装与激活
声明:本文所提供的所有软件均来自于互联网,仅供个人研究和学习使用,请勿用于商业用途,下载后请于24小时内删除,请支持正版! 本文介绍Navicat Premium 12的安装.激活与基本使用.已于20 ...
- FLIR 相机采集程序
https://www.ptgrey.com/Downloads/GetSecureDownloadItem/11048 Grasshopper3 4.1 MP Mono USB3 Vision (C ...
- 【故障公告】推荐系统中转站撑爆服务器 TCP 连接引发的故障
上周五下午,我们在博客中部署了推荐系统,在博文下方显示“最新IT新闻”的地方显示自动推荐的关联博文.我们用的推荐系统是第四范式的推荐服务,我们自己只是搭建了一个推荐系统中转站(基于 ASP.NET C ...
- Linux系统下Mysql安装与配置
一,使用系统 Centos7: 在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 二,下载安装 ...
- Vue 实现左边导航栏且右边显示具体内容(element-ui)
最终效果图: 现在开始进入正题: 1.安装element-ui npm i element-ui -S CDN 目前可以通过 unpkg.com/element-ui 获取到最新版本的资源,在页面上引 ...
- 队列(FIFO)—循环队列、队列的链式存储
1 队列的定义 队列是只允许在一端(队尾)进行插入操作,而在另一端(队头)进行删除操作的线性表. 2 队列的特点 1)先进先出是队列最大的特点,是应用中非常常见的模型,例如排队: 2)队列也属于线性表 ...
- Python openpyxl : Excel 文档简单操作
安装方法 使用 pip 或通过专门python IDE(如pyCharm)进行安装 其中pip安装方法,命令行输入: pip install openpyxl 基本使用 第一步先是要导入 openp ...