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
啊啊啊啊啊啊啊,真的是智障了... 这种题目,没有必要纠结来源.只要知道它的结果的导致直接原因?反正这句话就我听的懂吧... ">>"/"&" ...
随机推荐
- Windows 组策略的生效规则
一般的继承与处理规则 若上层父OU的某个组策略项目被设定,但是其下层子OU未设定该项目,则下层子OU继承上层父OU的这个组策略项目设定值 若在下层子OU内的某个组策略项目被设定,则此设定值预设会覆盖由 ...
- linux使用Nginx搭建静态资源服务器
最近公司需要做一个宣传片播放 视频有点大 好几百M 就想到使用Nginx来代理静态资源,在过程中出现了一些问题,比如端口没开.访问是403等,没有成功,后面慢慢查找问题,才发现大部分博客资料的都不全 ...
- 关于 Angular 跨域请求携带 Cookie 的问题
在前端开发调试接口的时候都会遇到跨域请求的问题.传统的方式是使用 Nginx 反向代理解决跨域.比如所有接口都在 a.com 的域下,通过 Nginx 将所有请求代理到 a.com 的域下即可. 使用 ...
- 10 Django RESTful api 实现匿名访问
# views_send_code.py from rest_framework.permissions import AllowAny class MsgCodeViewSet(CreateMode ...
- Scrum Meeting 博客
笨拙软件工程 Scrum Meeting 博客汇总 一.Alpha阶段 [alpha阶段]第一次Scrum Meeting [alpha阶段]第二次Scrum Meeting [alpha阶段]第三次 ...
- 通过后缀名和MIME-TYPE检查实现文件类型校验
前言 文件上传是一个在开发中很常见的需求场景,通常出于安全考虑,我们会对上传的文件进行类型校验,其中常见的有后缀名校验,mime-type校验 话不多说,直接上代码 1.首先定义允许上传的文件类型白名 ...
- vue 倒计时组件
<template> <span> <i v-text="msg"></i> </span></template& ...
- 结巴分词出现AttributeError: 'float' object has no attribute 'decode'错误
将data转变为str格式 inputfile = 'comment2.csv'outputfile = 'comment2_cut.txt'datas = pd.read_csv(inputfile ...
- linux makefile中一些复制运算的区别
Makefile 中 :=. ?= .+= .=的区别 = 是最基本的赋值:= 是覆盖之前的值?= 是如果没有被赋值过就赋予等号后面的值,如果已经被赋值则就用之前的赋值+= 是添加等号后面的值
- iPhone 系统刷机
1. 下载好固件(爱思 或者 pp助手) e.g. http://jailbreak.25pp.com/gujian/ 2. 将电脑与手机连接上,弹出iTunes软件即可 3. 长按手机电源键 关闭手 ...