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(xA,yA) and point B(xB,yB) is |xA−xB|+|yA−yB|. Now he wants to find the maximum distance between two points of n points.
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≤109), 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); ```
``` 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
3 233
5 332
Sample Output
1557439953
1423870062
1423870062
Source
先附上自己的写法,运气好的话可以过,运气不好的话超时,这东西也看人品?
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1000006
#define inf 1e12 struct node{
ll x,y;
}e[N],res[N];
ll cmp(node a,node b)
{
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
ll cross(node a,node b,node c)//向量积
{
return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
ll convex(ll n)//求凸包上的点
{
sort(e,e+n,cmp);
ll m=,i,j,k;
//求得下凸包,逆时针
//已知凸包点m个,如果新加入点为i,则向量(m-2,i)必定要在(m-2,m-1)的逆时针方向才符合凸包的性质
//若不成立,则m-1点不在凸包上。
for(i=;i<n;i++)
{
while(m>&&cross(res[m-],e[i],res[m-])<=)m--;
res[m++]=e[i];
}
k=m;
//求得上凸包
for(i=n-;i>=;i--)
{
while(m>k&&cross(res[m-],e[i],res[m-])<=)m--;
res[m++]=e[i];
}
if(n>)m--;//起始点重复。
return m;
} long long n,seed;
inline long long rand(long long l, long long r) {
static long long mo=1e9+, g=;
return l+((seed*=g)%=mo)%(r-l+);
} int main()
{
int t;
scanf("%d",&t);
while(t--){
cin >> n >> seed;
for (int i = ; i < n; i++){
e[i].x = rand(-, ),
e[i].y = rand(-, );
}
ll m=convex(n);
ll ans=-;
for(ll i=;i<m;i++){
for(ll j=i+;j<m;j++){
ll cnt = abs(res[i].x-res[j].x)+abs(res[i].y-res[j].y);
ans=max(ans,cnt);
}
}
printf("%I64d\n",ans); }
return ;
}
官方题解:
#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a)) using namespace std; typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii; inline int in()
{
int res=;char c;int f=;
while((c=getchar())<'' || c>'')if(c=='-')f=-;
while(c>='' && c<='')res=res*+c-'',c=getchar();
return res*f;
}
const int N = ; ll a[N][];
int n;
long long seed;
inline long long rand(long long l, long long r) {
static long long mo=1e9+, g=;
return l+((seed*=g)%=mo)%(r-l+);
}
int main() {
int T;
for (scanf("%d", &T);T--;) {
cin >> n >> seed;
for (int i=; i<n; i++)
a[i][]=rand(-, ),
a[i][]=rand(-, );
ll t=;
ll ans=,mx=-9223372036854775808LL,mn=9223372036854775807LL;
for (int s=; s<(<<); s++) {
mx=-9223372036854775808LL,mn=9223372036854775807LL;
for (int i=; i<n; i++) {
t = ;
for (int j=; j<; j++)
if ((<<j) & s) t += a[i][j];
else t -= a[i][j];
mn = min(mn, t);
mx = max(mx, t);
}
ans = max(ans, mx-mn);
}
printf("%I64d\n", ans);
}
return ;
}