题目: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]----剩下的。你懂得~~~

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <string>
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <cmath>
  8. #include <map>
  9. #include <set>
  10. #include <queue>
  11. using namespace std;
  12.  
  13. #define ls(rt) rt*2
  14. #define rs(rt) rt*2+1
  15. #define ll long long
  16. #define ull unsigned long long
  17. #define rep(i,s,e) for(int i=s;i<e;i++)
  18. #define repe(i,s,e) for(int i=s;i<=e;i++)
  19. #define CL(a,b) memset(a,b,sizeof(a))
  20. #define IN(s) freopen(s,"r",stdin)
  21. #define OUT(s) freopen(s,"w",stdout)
  22. const ll ll_INF = ((ull)(-1))>>1;
  23. const int INF = 100000000;
  24. const double EPS = 1e-8;
  25.  
  26. int ABS(int x)
  27. {
  28. return x>=0?x:(-x);
  29. }
  30.  
  31. int a[10][4][2];
  32. int n,m,k,s;
  33.  
  34. int main()
  35. {
  36. //IN("in.txt");
  37. int x,y;
  38. while(~scanf("%d%d%d%d",&n,&m,&k,&s))
  39. {
  40. for(int i=0;i<10;i++)
  41. for(int j=0;j<4;j++)
  42. {
  43. a[i][j][0]=INF;//0 min
  44. a[i][j][1]=-INF;//1 max
  45. }
  46. //printf("cap\n");
  47. for(int i=0;i<n;i++)
  48. for(int j=0;j<m;j++)
  49. {
  50. scanf("%d",&x);
  51. a[x][0][0]=min(a[x][0][0],-i-j);
  52. a[x][0][1]=max(a[x][0][1],-i-j);
  53. a[x][1][0]=min(a[x][1][0],-i+j);
  54. a[x][1][1]=max(a[x][1][1],-i+j);
  55. a[x][2][0]=min(a[x][2][0],i-j);
  56. a[x][2][1]=max(a[x][2][1],i-j);
  57. a[x][3][0]=min(a[x][3][0],i+j);
  58. a[x][3][1]=max(a[x][3][1],i+j);
  59. }
  60. int ans=0;
  61. scanf("%d",&x);
  62. for(int i=1;i<s;i++)
  63. {
  64. scanf("%d",&y);
  65. for(int j=0;j<4;j++)
  66. {
  67. ans=max(ans,ABS(a[x][j][1]-a[y][j][0]));
  68. ans=max(ans,ABS(a[x][j][0]-a[y][j][1]));
  69. }
  70. x=y;
  71. }
  72. printf("%d\n",ans);
  73. }
  74. return 0;
  75. }

还看到还有一种做法也是不错的: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

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdlib>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<string>
  8. #include<map>
  9. #include<set>
  10. #include<vector>
  11. #include<queue>
  12. #include<stack>
  13. using namespace std;
  14. #define rep(i, a, b) for (int i = (a); i <= (b); ++i)
  15. #define red(i, a, b) for (int i = (a); i >= (b); --i)
  16. #define clr( x , y ) memset(x,y,sizeof(x))
  17. #define sqr(x) ((x) * (x))
  18. typedef long long lint;
  19. int n,m,k,s,x,y,a[10][5];
  20.  
  21. void init()
  22. {
  23. scanf("%d%d%d%d",&n,&m,&k,&s);
  24. clr(a,243);
  25. rep(i,1,n)
  26. rep(j,1,m) {
  27. scanf("%d",&x);
  28. a[x][0] = max( a[x][0] , -i - j );
  29. a[x][1] = max( a[x][1] , -i + j );
  30. a[x][2] = max( a[x][2] , i - j );
  31. a[x][3] = max( a[x][3] , i + j );
  32. }
  33. }
  34.  
  35. void work()
  36. {
  37. int ans = -100000000;
  38. scanf("%d",&x);
  39. rep(i,2,s) {
  40. scanf("%d",&y);
  41. rep(j,0,3) ans = max( ans , a[x][j] + a[y][3-j] );
  42. x = y;
  43. }
  44. cout<<ans;
  45. }
  46.  
  47. int main()
  48. {
  49. init();
  50. work();
  51. return 0;
  52. }

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奶酪:【数据结构与算法】搜索之BFS

    1.目标 通过本文,希望可以达到以下目标,当遇到任意问题时,可以: 1.很快建立状态空间: 2.提出一个合理算法: 3.简单估计时空性能: 2.搜索分类 2.1.盲目搜索 按照预定的控制策略进行搜索, ...

  2. Appium Python 五:元素定位

    总结 单个元素定位: driver.find_element_by_accessibility_id(id) driver.find_element_by_android_uiautomator(ui ...

  3. uva 10670 Work Reduction(贪心)

    题目连接:10670 - Work Reduction 题目大意:有tol的工作量,和要求达到的工作剩余量sur,然后是公司总数,对应每个公司提供两种服务,1.完成一个工作量,2.完成当前未完成工作量 ...

  4. 设计模式——门面模式(Facade)

    要想正确理解设计模式,首先必须明白它是为了解决什么问题而提出来的. 设计模式学习笔记 --Shulin 转载请注明出处:http://blog.csdn.net/zhshulin 1.概念 门面模式是 ...

  5. PHP 在Win下的安装

    1:安装集成环境,Wamp或者Appserv.可以快速搭建测试环境. 2:分别下载安装 下载 PHP 从此处下载免费的 PHP:http://www.php.net/downloads.php 下载 ...

  6. eclipse下java中凝视字体太小和xml中中文字体太小问题解决方法

    我们在win7下进行android应用开发.须要搭建对应的开发环境.如今普遍基本上都是eclipse+adt+sdk,在本人搭建完环境后,发现eclipse下.java中的凝视和xml中的中文字体变得 ...

  7. java 如何查看jdk版本&位数

      java 如何查看jdk版本&位数 CreateTime--2018年4月22日18:20:18 Author:Marydon 方式一:通过dos命令实现 win+R-->cmd-- ...

  8. 〖Linux〗录像桌面视频同时录音

    1. 安装依赖的包 sudo apt-get install -y ffmpeg oss-compat alsa-oss 2. 录制桌面视频并录音 aoss ffmpeg -f oss -i /dev ...

  9. python之smtplib模块 发送邮件

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #smtplib模块 发送邮件 import smtplib from email.mime.text imp ...

  10. python之模块csv之 读取CSV文件(reader和DictReader2个方法)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #读取CSV文件(reader和DictReader2个方法) import csv #csv文件,是一种常用 ...