CodeForces 463C - Gargari and Bishops
2015-07-27 11:00
405 查看
Gargari is jealous that his friend Caisa won the game from the previousproblem. He wants to prove that he is a genius.
He has a n × n chessboard. Each cell
of the chessboard has a number written on it.Gargari wants to place two bishops on the chessboard in such a way that thereis no cell that is attacked by both of them. Consider a cell with number x written
on it, if this cell is attacked by oneof the bishops Gargari will get x dollars for it. Tell Gargari, how to
place bishops on the chessboard toget maximum amount of money.
We assume a cell is attacked by a bishop, if the cell is located on thesame diagonal with the bishop (the cell, where the bishop is, also consideredattacked by it).
Input
The first line contains a single integer n (2 ≤ n ≤ 2000).
Each of the next n lines contains n integers aij (0 ≤ aij ≤ 109) —
description of thechessboard.
Output
On the first line print the maximal number of dollars Gargari will get.On the next line print four integers: x1, y1, x2, y2(1 ≤ x1, y1, x2, y2 ≤ n),
where xi is the number of the row where the i-th
bishop should be placed, yi is thenumber of the column where
the i-th bishop should be placed. Consider rows are numbered from 1 to n from
top to bottom, and columns are numberedfrom 1 to n from left to right.
If there are several optimal solutions, you can print any of them.
Sample test(s)
input
4
1 1 1 1
2 1 1 0
1 1 1 0
1 0 0 1
output
12
2 2 3 2
思路:
用f1,f2两个数组分别存储反对角线,正对角线上的数的和:
f1[i + j] += a[i][j];
f2[i - j + n] += a[i][j];
然后点(i,j)上两条对角线的和便为
sum[i][j] = f1[i + j] + f2[i - j + n] - a[i][j];
比赛时候没看到Gargari wants to place two bishops on thechessboard in such a way that there is no cell that is attacked by both ofthem.即两只棋子对角线不会有交集-.-
He has a n × n chessboard. Each cell
of the chessboard has a number written on it.Gargari wants to place two bishops on the chessboard in such a way that thereis no cell that is attacked by both of them. Consider a cell with number x written
on it, if this cell is attacked by oneof the bishops Gargari will get x dollars for it. Tell Gargari, how to
place bishops on the chessboard toget maximum amount of money.
We assume a cell is attacked by a bishop, if the cell is located on thesame diagonal with the bishop (the cell, where the bishop is, also consideredattacked by it).
Input
The first line contains a single integer n (2 ≤ n ≤ 2000).
Each of the next n lines contains n integers aij (0 ≤ aij ≤ 109) —
description of thechessboard.
Output
On the first line print the maximal number of dollars Gargari will get.On the next line print four integers: x1, y1, x2, y2(1 ≤ x1, y1, x2, y2 ≤ n),
where xi is the number of the row where the i-th
bishop should be placed, yi is thenumber of the column where
the i-th bishop should be placed. Consider rows are numbered from 1 to n from
top to bottom, and columns are numberedfrom 1 to n from left to right.
If there are several optimal solutions, you can print any of them.
Sample test(s)
input
4
1 1 1 1
2 1 1 0
1 1 1 0
1 0 0 1
output
12
2 2 3 2
思路:
用f1,f2两个数组分别存储反对角线,正对角线上的数的和:
f1[i + j] += a[i][j];
f2[i - j + n] += a[i][j];
然后点(i,j)上两条对角线的和便为
sum[i][j] = f1[i + j] + f2[i - j + n] - a[i][j];
比赛时候没看到Gargari wants to place two bishops on thechessboard in such a way that there is no cell that is attacked by both ofthem.即两只棋子对角线不会有交集-.-
代码: #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int N = 2005; __int64 sum ; __int64 f1[N * 2], f2[N * 2]; int a[N * 2][N * 2]; int main() { int n; memset(f1, 0, sizeof(f1)); memset(f2, 0, sizeof(f2)); scanf("%d", &n); int i; for (i = 0; i<n; i++) { int j; for (j = 0; j<n; j++) { scanf("%d", &a[i][j]); f1[i + j] += a[i][j]; f2[i - j + n] += a[i][j]; } } for (i = 0; i<n; i++) for (int j = 0; j<n; j++) sum[i][j] = f1[i + j] + f2[i - j + n] - a[i][j]; int x1, y1, x2, y2; __int64 max1 = 0, max2 = 0; x1 = x2 = y1 = 1; y2 = 2; for (i = 0; i<n; i++) for (int j = 0; j<n; j++) { if ((i + j) & 1) { if (max1<sum[i][j]) { max1 = sum[i][j]; x1 = i; y1 = j; } } else { if (max2<sum[i][j]) { max2 = sum[i][j]; x2 = i; y2 = j; } } } printf("%I64d\n", max1 + max2); printf("%d %d %d %d\n", x1 + 1, y1 + 1, x2 + 1, y2 + 1); return 0; }
相关文章推荐
- 修改Linux内核参数提高Nginx服务器并发性能
- 每天一个linux命令:ln 命令
- Linux进程编程
- Linux Shell: 统计系统中占用Swap 的程序PID和占用大小
- Course Schedule II
- 关于@property的一些小总结
- linux设置系统时间
- linux设置系统时间
- 深入理解Linux内核---进程
- 浅谈 linux 多线程编程和 windows 多线程编程的异同
- 隐藏nginx 版本号信息
- 对于用u盘安装CentOS后不插u盘就无法开启的解决过程
- MAC-Zsh安装与使用——终极Shell
- ubuntu下如何整合apache2与tomcat
- linux下查看文件大小的命令
- (转)nginx+iis实现负载均衡
- Nginx日志切割
- LINUX关闭防火墙
- LINUX关闭防火墙
- nginx 重写 rewrite 基础