Dima and Magic Guitar CodeForces, dimacodeforces

Source: Internet
Author: User

Dima and Magic Guitar CodeForces, dimacodeforces

Dima and Magic Guitar CodeForces-certificate e

Question:

Http://blog.csdn.net/u011026968/article/details/38716425
Http://vawait.com/2013/11/codeforces-366e/
Http://www.cnblogs.com/jianglangcaijin/archive/2013/11/25/3441319.html

For any two adjacent numbers x and y in s, it is required to find any two points in the rectangle that are equal to x and y respectively, and then calculate their Manhattan distance, this question requires that the maximum distance of all the obtained Manhattan is the maximum. It is easy to think of, it should be to make one-to-point Manhattan distance the largest, other points can be arbitrary. That is, for all the adjacent two numbers in s, find the two points in the rectangle that are equal to the two numbers and have the largest distance between Manhattan.

The distance from Manhattan is equal to the following maximum:

(Xa-xb) + (ya-yb)
(Xa-xb)-(ya-yb)
-(Xa-xb) + (ya + yb)
-(Xa-xb)-(ya-yb)

That is, the maximum values of these values:

(Xa + ya)-(xb + yb)
(Xa-ya)-(xb-yb)
(-Xa + ya)-(-xb + yb)
(-Xa-ya)-(-xb-yb)

Therefore, the maximum Manhattan distance between the points a and B is required, that is, the maximum values of these four types. The maximum values of each type are the maximum and the minimum. That is, the maximum and minimum values of xa + ta, xa-ya,-xa + ya, and-xa-ya in all the points whose values are a are recorded respectively.

(This question is not about how to handle the problem when it is impossible, nor does it have such data .)

 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int a[100110]; 6 int max1[11][4],min1[11][4]; 7 int n,m,k,s,ans; 8 int main() 9 {10     int i,j,t;11     scanf("%d%d%d%d",&n,&m,&k,&s);12     memset(min1,0x3f,sizeof(min1));13     memset(max1,140,sizeof(max1));14     for(i=1;i<=n;i++)15         for(j=1;j<=m;j++)16         {17             scanf("%d",&t);18             max1[t][0]=max(max1[t][0],i+j);19             max1[t][1]=max(max1[t][1],i-j);20             max1[t][2]=max(max1[t][2],-i+j);21             max1[t][3]=max(max1[t][3],-i-j);22             min1[t][0]=min(min1[t][0],i+j);23             min1[t][1]=min(min1[t][1],i-j);24             min1[t][2]=min(min1[t][2],-i+j);25             min1[t][3]=min(min1[t][3],-i-j);26         }27     scanf("%d",&a[1]);28     for(i=2;i<=s;i++)29     {30         scanf("%d",&a[i]);31         for(j=0;j<=3;j++)32             ans=max(ans,max(max1[a[i-1]][j]-min1[a[i]][j],max1[a[i]][j]-min1[a[i-1]][j]));33     }34     printf("%d",ans);35     return 0;36 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.