CodeForces 173C Spiral Maximum (想法、模拟)
Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
Let's consider a k × k square, divided into unit squares. Please note that k ≥ 3 and is odd. We'll paint squares starting from the upper left square in the following order: first we move to the right, then down, then to the left, then up, then to the right again and so on. We finish moving in some direction in one of two cases: either we've reached the square's border or the square following after the next square is already painted. We finish painting at the moment when we cannot move in any direction and paint a square. The figure that consists of the painted squares is a spiral.
The figure shows examples of spirals for k = 3, 5, 7, 9.
You have an n × m table, each of its cells contains a number. Let's consider all possible spirals, formed by the table cells. It means that we consider all spirals of any size that don't go beyond the borders of the table. Let's find the sum of the numbers of the cells that form the spiral. You have to find the maximum of those values among all spirals.
Input
The first line contains two integers n and m (3 ≤ n, m ≤ 500) — the sizes of the table.
Each of the next n lines contains m space-separated integers: the j-th number in the i-th line aij ( - 1000 ≤ aij ≤ 1000) is the number recorded in the j-th cell of the i-th row of the table.
Output
Print a single number — the maximum sum of numbers among all spirals.
Sample Input
6 5
0 0 0 0 0
1 1 1 1 1
0 0 0 0 1
1 1 1 0 1
1 0 0 0 1
1 1 1 1 1
17
3 3
1 1 1
1 0 0
1 1 1
6
6 6
-3 2 0 1 5 -1
4 -1 2 -3 0 1
-5 1 2 4 1 -2
0 -2 1 3 -1 2
3 1 4 -3 -2 0
-1 2 -1 3 1 2
13
Hint
In the first sample the spiral with maximum sum will cover all 1's of the table.
In the second sample the spiral may cover only six 1's.
【题意】:
在m*n的表中找出权值和最小的螺旋线。
【解题思路】:
螺旋线的个数上限为500*500*250。
观察可以看到 相邻两个螺旋线再加上一个小方格就可以组成一个方阵。
只要预处理出方阵的权值和,就可以在O(1)内从一个螺旋线到另一个螺旋线。
这里的枚举方式是:枚举中心点,以中心点为基准向外依次拓展螺旋线(对应K值递增)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#define LL long long
#define maxn 505
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int n,m;
int val[maxn][maxn];
int row[maxn][maxn];
int col[maxn][maxn]; void input(){
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
scanf("%d",&val[i][j]);
for(int i=; i<=n; i++){
row[i][] = ;
for(int j=; j<=m; j++){
row[i][j] = row[i][j-]+val[i][j];
}
}
for(int i=; i<=m; i++){
col[i][] = ;
for(int j=; j<=n; j++){
col[i][j] = col[i][j-]+val[j][i];
}
}
} bool is_ok(int x, int y){
return x>= && y>= && x<=n &&y<=m;
} int main(int argc, char const *argv[])
{
//IN; while(scanf("%d %d",&n,&m)!=EOF)
{
input(); int ans = -inf;
for(int i=; i<=n; i++){
for(int j=; j<=m; j++){ //center
int cur = val[i][j];
int tol = val[i][j];
int ex = i, ey = j-;
int x1 = i-, y1 = j-;
int x2 = i+, y2 = j+;
while(is_ok(x1,y1) && is_ok(x2,y2) && is_ok(ex,ey)){
tol += row[x1][y2] - row[x1][y1-];
tol += row[x2][y2] - row[x2][y1-];
tol += col[y1][x2-] - col[y1][x1];
tol += col[y2][x2-] - col[y2][x1]; cur = tol - cur - val[ex][ey];
ans = max(ans, cur); ex = x1; ey = y1-;
x1 = x1-, y1 = y1-;
x2 = x2+, y2 = y2+;
} }
} printf("%d\n", ans);
} return ;
}
CodeForces 173C Spiral Maximum (想法、模拟)的更多相关文章
- CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化
Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...
- CodeForces.158A Next Round (水模拟)
CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...
- Codeforces Round #577 (Div. 2) C. Maximum Median (模拟,中位数)
题意:给你一个长度为奇数\(n\)的序列.你可以对任意元素加上\(k\)次\(1\),求操作后的中位数最大. 题解:先对序列进行排序,然后对中位数相加,如果中位数和后面的元素相等,就对后面所有和当前中 ...
- CodeForces 670 A. Holidays(模拟)
Description On the planet Mars a year lasts exactly n days (there are no leap years on Mars). But Ma ...
- CodeForces - 586C Gennady the Dentist 模拟(数学建模的感觉)
http://codeforces.com/problemset/problem/586/C 题意:1~n个孩子排成一排看病.有这么一个模型:孩子听到前面的哭声自信心就会减弱:第i个孩子看病时会发出v ...
- Codeforces 747C:Servers(模拟)
http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开 ...
- Codeforces 740A. Alyona and copybooks 模拟
A. Alyona and copybooks time limit per test: 1 second memory limit per test: 256 megabytes input: st ...
- Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))
A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces 280D k-Maximum Subsequence Sum [模拟费用流,线段树]
洛谷 Codeforces bzoj1,bzoj2 这可真是一道n倍经验题呢-- 思路 我首先想到了DP,然后矩阵,然后线段树,然后T飞-- 搜了题解之后发现是模拟费用流. 直接维护选k个子段时的最优 ...
随机推荐
- hdu 4941 Magical Forest ( 双重map )
题目链接 题意: 有一个n*m的田地,里边有k棵树,每棵树的位置为(xi,yi),含有能量值ci.之后又q个询问,分三种; 1)1 a b,将a行和b行交换 2)2 a b,将a列和b列交换 3)3 ...
- bzoj1296: [SCOI2009]粉刷匠
dp. 用到俩次dp,用1和0代表俩种颜色,首先对于每块木板我们进行一次dp,g[i][j]代表前j个格子刷i次最多能涂到几个格子. 则 g[i][j]=max(g[i-1][k],max(cnt[j ...
- java分层架构概念
转自:http://www.cnblogs.com/bdqnbenet/p/4924778.html service是业务层 DAO (Data Access Object) 数据访问 1.JAVA中 ...
- hdu 4614 Vases and Flowers(线段树:成段更新)
线段树裸题.自己写复杂了,准确说是没想清楚就敲了. 先是建点为已插花之和,其实和未插花是一个道理,可是开始是小绕,后来滚雪球了,跪了. 重新建图,分解询问1为:找出真正插画的开始点和终止点,做成段更新 ...
- I.MX6 linux Qt 同时支持Touch、mouse
/***************************************************************************** * I.MX6 linux Qt 同时支持 ...
- <八>面向对象分析之UML核心元素之分析类
一:基本概念 ---->在那大数项目中,分析类是被忽视的一种非常有用的元素. ---->分析类用于获取系统中主要的“职责簇”,他们代表系统的原型类,是系统必须处 ...
- 【CSS】使用CSS改变超链接样式
超链接代码 <ahrefahref="http://www.divCSS5.com/"target="_blank" title="关于divC ...
- Oracle RAC环境下如何更新patch(Rolling Patch)
Oracle RAC数据库环境与单实例数据库环境有很多共性,也有很多异性.对于数据库补丁的更新同样如此,都可以通过opatch来完成.但RAC环境的补丁更新有几种不同的更新方式,甚至于可以在零停机的情 ...
- Oracle 一次 锁表 处理小记
同事说测试库上的一张表被锁了. 不能执行DML 操作. 锁表的准确说法应该是阻塞.之前的一遍blog里有说明: 锁 死锁 阻塞Latch 等待 详解 http://blog.csdn.net/tian ...
- 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)
转:https://github.com/kimziv/PinYin4Objc 最好用的汉字转拼音代码PinYin4Objc(PinYin4J的objc版本)(更新到v1.1.1,增加block异步处 ...