寻找串S中的每个数字在给定的矩阵中的位置,使S中相邻的两个数的曼哈顿距离最大,并输出相邻的数的最大曼哈顿距离。。。。(题目真难懂)
| x1 - x2 | - | y1 - y2 |=以下4种情况下的最大值
one:
x1-x2+y1-y2 ---> (x1+y1) - (x2+y2)
two:
-(x1-x2) + (y1-y2) ---> (x2-y2) - (x1-y1)
three:
(x1-x2) - (y1-y2) ---> (x1-y1) - (x2-y2)
four:
-( x1-x2 ) - (y1-y2) ---> (x2+y2) - (x1+y1)
曼哈顿距离可以用两个点的纵横坐标表示出来,对每个数字可以维护使减号前的值最大,减号后的值最小
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdio>
using namespace std;
int n,m,k,s;
int f[10][2],b[10][2];
int max5(int a,int b,int c,int d,int e)
{
return max(a,max(max(b,c),max(d,e)));
}
int main()
{
memset(f,192,sizeof(f));
memset(b,63,sizeof(b));
scanf("%d%d%d%d",&n,&m,&k,&s);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int a;
scanf("%d",&a);
f[a][0]=max(f[a][0],i+j); f[a][1]=max(f[a][1],i-j);
b[a][0]=min(b[a][0],i+j); b[a][1]=min(b[a][1],i-j);
}
}
int p,q,ans=0;
for(int i=0;i<s;i++,p=q)
{
scanf("%d",&q);
if(i)
{
int t1=f[p][0]-b[q][0];
int t2=f[q][1]-b[p][1];
int t3=f[p][1]-b[q][1];
int t4=f[q][0]-b[p][0];
ans=max5(ans,t1,t2,t3,t4);
}
}
printf("%d\n",ans);
return 0;
}
分享到:
相关推荐
Codeforces round 678 D2_Codeforces_源码
codeforces每日一题。 题意: 给出一个数组,让你挑选出能组成任意pair差值为2的幂的序列,并且使这个序列长度尽可能大。 思路: 针对于挑选出来的序列,任意pair对的差值为2的幂数。 假设有多个pair对,设dis(a,b)=2...
Codeforces Round #632 (Div. 2) C. Eugene and an array 题意: 求出一个数列中子区间满足 此区间的任意子区间之和 不为0的区间个数。 思路: 考虑用dp[x]dp[x]dp[x]记录前缀和为xxx的区间右端点。 那么这道题其实...
E. Array Shrinking time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are given an array a1,a2,…,an. You can perform the following operation...
Watermelon: http://codeforces.com/problemset/problem/4/A 2- 71A. Way Too Long Words: http://codeforces.com/problemset/problem/71/A 3- 118A. String Task: ...
题意: 给出 nnn 个点,n−1n-1n−1 条边,最多询问 n2\frac{n}{2}2n 次,每次询问 u,vu,vu,v,会给出 uvuvuv的最近公共祖先,求树的根。 ...操作就是一个删除叶子节点的过程。 AC代码: const int N = 1010;...
题意: 在集合 S=1,2,⋯,nS={1,2,⋯,n}S=1,2,⋯,n 中,对于每个正整数 kkk ,找出一个大小为 kkk 的子集,使得该子集中两两间最大公因数的最大值最小,求这个最小值。 我们考虑如何构造两两间最大公因数的最大值最小...
题意: 有n个人m个敌对关系, 一开始第x个人的工资是x 有q次操作,每次操作会给一个人的编号x 然后将x的工资变为所有人中最多的。 如果a和b是敌对关系,a的工资比b高,那么a会嘲讽 如果a嘲讽b,b嘲讽c,那么(a,b,...
Codeforces - 1107B. Digital root & 1107C. Brutality(规律 & 贪心)Codeforces - 1107B.
codeforces 19 E Fairy 一道比较难的题目的解题报告 推荐阅读
codeforces每日一练。 题意: 给一棵树,每个点有一个点权,每条边有一个边权,求一条链使得点权和-边权和最大。 思路: 由于我没看清楚题意,以为是求联通子图的点权和-边权和最大,用link-cut-tree写换根,wa10了两...
Educational Codeforces Round 157D. XOR Construction
codeforces.dev codeforces.dev 这是应用程序的项目模板。 它位于 。 要使用基于此模板创建一个新项目: npx degit sveltejs/template svelte-app cd svelte-app 请注意,您将需要安装 开始吧 安装依赖项... ...
codeforces每日一练。 题意: 有n张卡片,卡片上的数字就是分数,比如说甲乙两人抽卡,三局两胜,一局得分高的胜,求在甲赢了两局的情况下乙赢了第三局且总分比甲高的概率。 思路: 数据1e3,很明显的On^2算法,所以...
F. Maximum White Subtree time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are given a tree consisting of n vertices. A tree is a connected ...
codeforces每日一练。 题意: 给定n个点,m条有向边,以及k时间。求不超过k时间1-n最多能经过多少个点。 思路: 数据<=5000,说明是个暴力dp。 那么可以用dp[i][j]维护从1到i点经过了j个点,然后初始化为inf,再设...
Codeforces Guide.zip
一个简单的基于ASP.net的个人聊天室网页。
D. Ehab the Xorcist ...Given 2 integers u and v, find the shortest array such that bitwise-xor of its elements is u, and the sum of its elements is v. Input The only line contains 2 integers u and v
Codeforces - 1131C. Birthday(贪心)题目链接题目给你n和n个数,要你重新排列n个数,使得这些数的相邻差值中最大的那个值最小。stat