题目:http://codeforces.com/problemset/problem/366/E

事实上就是找 n * m 矩阵中数字 x 和 数字 y 的最远距离。

方法參照武森的论文《浅谈信息学中的“0”和“1”》

先约定符号:xi,xj  (i,j)是x的下标,当然。矩阵中的值是能够反复的

上面是武森的论文原文。加上我之前的符号约定,我在做点解释:

事实上那个max={四种可能}  更好的写法是:

|xi-yi|+|xj-yj|=max((1),(2),(3),(4))

(1)(xi+xj)-(yi+yj)   就是3-3  最大就是max3-min3

(2)(xi-xj)-(yi-yj)      2-2  最大就是max2-min2

(3)(-xi+xj)-(-yi+yj)  1-1  最大就是max1-min1

(4)(-xi-xj)-(-yi-yj)    0-0  最大就是max0-min0

那么维护数组num[v][4][2]    num[v][4][0]   就是0 1 2 3 情况下的最小值。num[v][4][1]   就是0 1 2 3 情况下的最大值。由于v能够出如今矩阵的多个位置就是说矩阵能够有反复值。所以维护的[0] [1]可能不是一个坐标处的,可是也是能够的

这么解释应该都能理解,然后假设是多维。只是是维护num[v][8][2]----剩下的。你懂得~~~

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std; #define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const int INF = 100000000;
const double EPS = 1e-8; int ABS(int x)
{
return x>=0?x:(-x);
} int a[10][4][2];
int n,m,k,s; int main()
{
//IN("in.txt");
int x,y;
while(~scanf("%d%d%d%d",&n,&m,&k,&s))
{
for(int i=0;i<10;i++)
for(int j=0;j<4;j++)
{
a[i][j][0]=INF;//0 min
a[i][j][1]=-INF;//1 max
}
//printf("cap\n");
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
scanf("%d",&x);
a[x][0][0]=min(a[x][0][0],-i-j);
a[x][0][1]=max(a[x][0][1],-i-j);
a[x][1][0]=min(a[x][1][0],-i+j);
a[x][1][1]=max(a[x][1][1],-i+j);
a[x][2][0]=min(a[x][2][0],i-j);
a[x][2][1]=max(a[x][2][1],i-j);
a[x][3][0]=min(a[x][3][0],i+j);
a[x][3][1]=max(a[x][3][1],i+j);
}
int ans=0;
scanf("%d",&x);
for(int i=1;i<s;i++)
{
scanf("%d",&y);
for(int j=0;j<4;j++)
{
ans=max(ans,ABS(a[x][j][1]-a[y][j][0]));
ans=max(ans,ABS(a[x][j][0]-a[y][j][1]));
}
x=y;
}
printf("%d\n",ans);
}
return 0;
}

还看到还有一种做法也是不错的:http://vawait.com/codeforces-366e/

事实上道理一样

(1)(xi+xj)-(yi+yj)=xi+xj+(-yi-yj)   就是3+0  最大就是max3+max0

(2)(xi-xj)+(-yi+yj)      2+1  最大就是max2+max1

(3)(-xi+xj)+(yi-yj)  1+2  最大就是max1+max2

(4)(-xi-xj)+(yi+yj)    0+3  最大就是max0+max3

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define red(i, a, b) for (int i = (a); i >= (b); --i)
#define clr( x , y ) memset(x,y,sizeof(x))
#define sqr(x) ((x) * (x))
typedef long long lint;
int n,m,k,s,x,y,a[10][5]; void init()
{
scanf("%d%d%d%d",&n,&m,&k,&s);
clr(a,243);
rep(i,1,n)
rep(j,1,m) {
scanf("%d",&x);
a[x][0] = max( a[x][0] , -i - j );
a[x][1] = max( a[x][1] , -i + j );
a[x][2] = max( a[x][2] , i - j );
a[x][3] = max( a[x][3] , i + j );
}
} void work()
{
int ans = -100000000;
scanf("%d",&x);
rep(i,2,s) {
scanf("%d",&y);
rep(j,0,3) ans = max( ans , a[x][j] + a[y][3-j] );
x = y;
}
cout<<ans;
} int main()
{
init();
work();
return 0;
}

CF 366E - Dima and Magic Guitar 最远曼哈顿距离的更多相关文章

  1. CF 366E Dima and Magic Guitar(最远哈密顿距离)

    题目链接:http://codeforces.com/problemset/problem/366/E 题意:给出一个n*m的数字矩阵A,每个矩阵元素的范围[1,K].给出一个长度为s的数字串B,B的 ...

  2. cf E. Dima and Magic Guitar

    http://codeforces.com/contest/366/problem/E |x1-x2|+|y1-y2|有四种情况 1.-(x1-x2)+(y1-y2); 2.(x1-x2)-(y1-y ...

  3. Dima and Magic Guitar CodeForces - 366E

    Dima and Magic Guitar CodeForces - 366E 题意: http://blog.csdn.net/u011026968/article/details/38716425 ...

  4. hdu 4666:Hyperspace(最远曼哈顿距离 + STL使用)

    Hyperspace Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  5. poj 2926:Requirements(最远曼哈顿距离,入门题)

    Requirements Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3908   Accepted: 1318 Desc ...

  6. POJ-2926 Requirements 最远曼哈顿距离

    题目链接:http://poj.org/problem?id=2926 题意:求5维空间的点集中的最远曼哈顿距离.. 降维处理,推荐2009武森<浅谈信息学竞赛中的“0”和“1”>以及&l ...

  7. [HDU 4666]Hyperspace[最远曼哈顿距离][STL]

    题意: 许多 k 维点, 求这些点之间的最远曼哈顿距离. 并且有 q 次操作, 插入一个点或者删除一个点. 每次操作之后均输出结果. 思路: 用"疑似绝对值"的思想, 维护每种状态 ...

  8. HDU 4666 Hyperspace (最远曼哈顿距离)

    Hyperspace Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  9. HDU 4666 最远曼哈顿距离

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4666 关于最远曼哈顿距离的介绍: http://blog.csdn.net/taozifish/ar ...

随机推荐

  1. ZH奶酪:PHP安装扩展imagick

    明明几个简单命令就能搞定,但是按照网上的方法就是不行,弄了一天,最后发现只需要两行命令,而且不需要修改什么php.ini: sudo apt-get install php5-imagick sudo ...

  2. Closing a window

    The obvious way to how to close a window is to click on the x mark on the titlebar. In the next exam ...

  3. eclipse 打包 apk 文件

    1.通过eclipse中的file ,点击Import 导入项目 2.选择Android 中的 Existing Android Code Into Workspace 3.通过 Brower 引入项 ...

  4. css 设置英文字母大小写转换(text-transform)

      css 设置英文字母大小写转换 CreateTime--2018年5月25日07点16分 Author:Marydon 1.实现:通过text-transform实现 2.text-transfo ...

  5. POJ2226

    题意:给定一个矩阵,当中有一些地方有水,如今有一些长度随意,宽为1的木板,要求在全部板不跨越不论什么坑的前提下,用一些木板盖住这些有水的地方,问至少须要几块板子? 思路: watermark/2/te ...

  6. mariadb多实例搭建

    测试环境基于centos7.2,腾讯云实验室,学习搭建! https://www.qcloud.com/developer 多实例mysql,能更加理解mysql安装的基本过程!及简单使用... ma ...

  7. QQ概念版(WPF制作)

    984 QQ概念版 编辑   QQ 概念版是腾讯首款NUI(自然用户交互)产品,全面实现了多点触摸操作.是腾讯利用微软最新一代的客户端展现层技术--WPF,打造的IM产品. 中文名 QQ 概念版 游戏 ...

  8. 【laravel5.4】中jquery的post Ajax提交

    1.post的ajax需要提交csrf_token字段,进行安全过滤 <meta name="csrf-token" content="{{ csrf_token( ...

  9. 转:PHP关于反斜杠处理函数addslashes()和stripslashes()的用法

    1.php处理\函数:addslashes()和stripslashes()函数 addslashes():对输入字符串中的某些预定义字符前添加反斜杠,这样处理是为了数据库查询语句等的需要.这些预定义 ...

  10. PHP-各种下载安装

    Windows下常用PHP扩展下载: http://dev.freshsite.pl/php-accelerators.html http://windows.php.net/downloads/pe ...