Clarke and points
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 433 Accepted Submission(s): 304
Problem Description
Clarke is a patient with multiple personality disorder. One day he turned into a learner of geometric.
He did a research on a interesting distance called Manhattan Distance. The Manhattan Distance between point A(x
A
,y
A
)
and point
B(x
B
,y
B
)
is
|x
A
−x
B
|+|y
A
−y
B
|
.
Now he wants to find the maximum distance between two points of n
points.
He did a research on a interesting distance called Manhattan Distance. The Manhattan Distance between point A(x
Now he wants to find the maximum distance between two points of n
Input
The first line contains a integer
T(1≤T≤5)
, the number of test case.
For each test case, a line followed, contains two integers n,seed(2≤n≤1000000,1≤seed≤10
9
)
, denotes the number of points and a random seed.
The coordinate of each point is generated by the followed code.
```
long long seed;
inline long long rand(long long l, long long r) {
static long long mo=1e9+7, g=78125;
return l+((seed*=g)%=mo)%(r-l+1);
}
// ...
cin >> n >> seed;
for (int i = 0; i < n; i++)
x[i] = rand(-1000000000, 1000000000),
y[i] = rand(-1000000000, 1000000000);
```
For each test case, a line followed, contains two integers n,seed(2≤n≤1000000,1≤seed≤10
The coordinate of each point is generated by the followed code.
```
long long seed;
inline long long rand(long long l, long long r) {
static long long mo=1e9+7, g=78125;
return l+((seed*=g)%=mo)%(r-l+1);
}
// ...
cin >> n >> seed;
for (int i = 0; i < n; i++)
x[i] = rand(-1000000000, 1000000000),
y[i] = rand(-1000000000, 1000000000);
```
Output
For each test case, print a line with an integer represented the maximum distance.
Sample Input
2 3 233 5 332
Sample Output
1557439953 1423870062
Source
题意:按照题意求出每点的坐标,然后求出最大的曼哈顿距离
思路:嗯,题目数据挺大的,按部就班的算一定超时,
看了人家博客,思路妙,
思路就是去绝对值符号,然后就有四种情况:
(x1-y2)-(x2-y2); (x1+y1)-(x2+Y2); (-x1+y1)-(-x2+y2); (-x1-y1)-(-x2-y2). 减号左右类似,所以对每个(x,y)求出四种情况的值,然后求出最值
代码;
#include<cstdio>
#include<algorithm>
#define LL long long
using namespace std;
LL n,seed;
struct node
{
LL x,y;
}a[1000010];
LL rand(LL l, LL r) {
static LL mo=1e9+7, g=78125;
return l+((seed*=g)%=mo)%(r-l+1);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&n,&seed);
for(int i=0;i<n;i++)
{
a[i].x = rand(-1000000000, 1000000000);
a[i].y = rand(-1000000000, 1000000000);
}
LL maxn[10],minn[10];
for(int i=0;i<6;i++)
{
maxn[i]=-1e16;minn[i]=1e16;
}
for(int i=0;i<n;i++)
{
maxn[0]=max(maxn[0],a[i
].x-a[i].y);
maxn[1]=max(maxn[1],a[i].x+a[i].y);
maxn[2]=max(maxn[2],-a[i].x+a[i].y);
maxn[3]=max(maxn[3],-a[i].x-a[i].y);
minn[0]=min(minn[0],a[i].x-a[i].y);
minn[1]=min(minn[1],a[i].x+a[i].y);
minn[2]=min(minn[2],-a[i].x+a[i].y);
minn[3]=min(minn[3],-a[i].x-a[i].y);
}
LL big=0;
for(int i=0;i<4;i++)
{
big=max(big,maxn[i]-minn[i]);
}
printf("%lld\n",big);
}
return 0;
}