Clarke and points

Problem Description
 
The Manhattan Distance between point A(XA,YA) and B(XB,YB) is |XA - XB| + |Xb - YB|;
the coordinate of each point is generated by the followed code.
Input

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

 
这道题原本不难但值得分析;开始一直在纠结两个变量之间的大小关系;因为这关系到去绝对值之后是否要变号的问题;但是还是看了题解。。。
题解:对于二维的变量,由于加了绝对值,则只有两种关系,要不就两个数要都大,这样ans = (Xi+Yi) - (Xj + Yj);同时注意到此时的| (Xi - Yi) - (Xj - Yj) | < | ans |;(两个正数相加肯定大于相减(加了绝对值也是一样),这时就算 计算了另一个对结果也没影响,后面亦同);若是一大一小,那么ans = (Xi - Yi) - (Xj - Yj);此时满足 |ans| > | (Xi+Yi) - (Xj + Yj) |;
这时能说ans就是取max( | (Xi+Yi) - (Xj + Yj) |,| (Xi - Yi) - (Xj - Yj) | );即维护最大最小的x+y与x - y;
 
ps:在线算法,节省空间;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
typedef __int64 ll;
template<typename T>
void read1(T &m)
{
T x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
template<typename T>
void read3(T &a,T &b,T &c){read1(a);read1(b);read1(c);}
template<typename T>
void out(T a)
{
if(a>) out(a/);
putchar(a%+'');
}
ll n,seed;
inline ll rand(ll l, ll r) {
static ll mo=1e9+, g=;
return l+((seed*=g)%=mo)%(r-l+);
}
const ll inf = 0x3f3f3f3f3f3f3f3fLL;
int main()
{
int T;
read1(T);
while(T--){
read2(n,seed);
ll mn0 = inf,mn1 = -inf,mx0 = inf,mx1 = -inf,x,y,ans;
rep0(i,,n){
x = rand(-, );
y = rand(-, );
ll tmp = x + y,temp = x - y;
mn0 = min(mn0,tmp);mn1 = max(mn1,tmp);
mx0 = min(mx0,temp);mx1 = max(mx1,temp);
}
ans = max(mn1 - mn0,mx1 - mx0);
out(ans);puts("");
}
return ;
}
 

hdu 5626 Clarke and points 数学推理的更多相关文章

  1. HDU 5626 Clarke and points 平面两点曼哈顿最远距离

    Clarke and points 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5626 Description Clarke is a patie ...

  2. hdu 5626 Clarke and points

    Problem Description Clarke is a patient with multiple personality disorder. One day he turned into a ...

  3. HDU 5628 Clarke and math——卷积,dp,组合

    HDU 5628 Clarke and math 本文属于一个总结了一堆做法的玩意...... 题目 简单的一个式子:给定$n,k,f(i)$,求 然后数据范围不重要,重要的是如何优化这个做法. 这个 ...

  4. HDU 5628 Clarke and math dp+数学

    Clarke and math 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5628 Description Clarke is a patient ...

  5. CF988 D. Points and Powers of Two【hash/数学推理】

    [链接]:CF [题意]:从一堆数中选一个最大子集,使得任意两个数相减的绝对值都是2的幂. [分析]:首先很难的一点,需要想到子集最多只能有三个,四个及以上的子集一定不存在(可以证明).当有三个元素时 ...

  6. hdu 5563 Clarke and five-pointed star 水题

    Clarke and five-pointed star Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/show ...

  7. hdu 4946 Just a Joke(数学+物理)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4969 Just a Joke Time Limit: 2000/1000 MS (Java/Others) ...

  8. hdu 5565 Clarke and baton 二分

    Clarke and baton Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  9. hdu 5465 Clarke and puzzle 二维线段树

    Clarke and puzzle Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

随机推荐

  1. svn :Can't connect to host *.*.*.*': 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

    Can't connect to host *.*.*.*': 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败. -------------------------------- ...

  2. iOS 手动打造JSON Model转换库

    前一段时间学习了Runtime,对类和对象的结构,和一些消息转发有一些自己的理解,现在希望简单的应用下,就决定自己写一个简单的JSON与Model的相互转化,现在总结下. 建议查看 参考资料 :Run ...

  3. org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge]

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFacto ...

  4. 16% off MPPS V16 ECU tuning tool for EDC15 EDC16 EDC17

    EOBD2.FR is offering 16% discount off the latest MPPS V16 ECU chip tuning tool. The device is now so ...

  5. 关于oozie

    什么是Oozie? Oozie是一个工作流引擎服务器,用于运行Hadoop Map/Reduce和Pig 任务工作流.同时Oozie还是一个Java Web程序,运行在Java Servlet容器中, ...

  6. 关于新feature对应的增加一个新的测试单子(QA)和文档单子(Doucmentation)的步骤

    一,增加一个new feature的文档单子. 1.new feature 增加对应的文档单子(公司有一个组是专门写产品说明的)所以增加一个新的功能就要有这个新的功能对应的一个文档(Documenta ...

  7. Linux 的使用基础---Linux的常用命令

    自己电脑的安装的软件太多了,如果重装linux系统的话,是在是太麻烦了,本身电脑系统是32位的,硬件上的配置也行,所以就安装了虚拟机,在虚拟机上又安装了一个Linuxd 系统,虽然速度是有些慢,总比浪 ...

  8. Javascript实现图片无缝滚动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. windows7 图形界面远程 centos6.5

    一.首先确定centos已经安装了gnome,因为centos自身没有图形界面,别说远程图形界面方式访问了,就本地图像界面方式都不行 1.首先查看系统的运行级别以及是否安装了桌面环境    1.使用命 ...

  10. MySQL之事务处理、存储过程

    事务处理 动作 开始事务:start transaction 提交事务:commit 回滚事务:rollback 设置自动提交:set autocommit 1 | 0 autoCommit系统默认是 ...