http://acm.hdu.edu.cn/showproblem.php?pid=4631

题意: 在平面内依次加点,求每次加点后最近点对距离平方的和

因为是找平面最近点对...所以加点以后这个最短距离一定是递减的...所以最后会形成这样一个函数图像

所以我们只要从后往前依次删点即可...

15秒惊险水过...不过我最小点对的木板肯定写挂了,卡时限的话估计过不了...

请用G++交...C++会TLE...当然我也无法解释这个问题...估计是我傻逼

/********************* Template ************************/
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std; #define EPS 1e-8
#define MAXN (int)5e5+5
#define MOD (int)1e9+7
#define PI acos(-1.0)
#define INF ((1LL)<<50)
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max3(a,b,c) (max(max(a,b),c))
#define min3(a,b,c) (min(min(a,b),c))
#define BUG cout<<"BUG! "<<endl
#define LINE cout<<"------------------"<<endl
#define L(t) (t << 1)
#define R(t) (t << 1 | 1)
#define Mid(a,b) ((a + b) >> 1)
#define lowbit(a) (a & -a)
#define FIN freopen("out.txt","w",stdout)
#pragma comment (linker,"/STACK:102400000,102400000") // typedef long long LL;
// typedef unsigned long long ULL;
typedef __int64 LL;
// typedef unisigned __int64 ULL;
// int gcd(int a,int b){ return b?gcd(b,a%b):a; }
// int lcm(int a,int b){ return a*b/gcd(a,b); } /********************* F ************************/
struct point
{
LL x,y;
int pos;
int id;
point(double a = ,double b = ,int c = ){
x = a ; y = b ; pos = c;
}
}p[MAXN],t[MAXN],tmp[MAXN]; LL n,ax,bx,cx,ay,by,cy; bool cmp(point a,point b){
if(a.x == b.x) return a.y < b.y;
return a.x < b.x;
} bool cmp1(point a,point b){
return a.y < b.y;
} LL dist(point a ,point b){
return (a.x-b.x) * (a.x-b.x) + (a.y-b.y) * (a.y-b.y);
} /*
* 二维空间找最近点对
* 返回排序后点位置的pair<int,int>
*/
pair<int,int> Closest_Pair(int l ,int r){
if(l == r || l+ == r) return make_pair(l,r); //1个点,2个点 直接return;
int m = Mid(l,r); // (l+r)/2
pair<int,int> dl = Closest_Pair(l,m);
pair<int,int> dr = Closest_Pair(m+,r);
LL ldis,rdis; //左部分的最值 右部分的最值
LL ans_dis; //左中右三部分最值 if(dl.first == dl.second) ldis = INF; //判重
else ldis = dist(p[dl.first],p[dl.second]); if(dr.first == dr.second) rdis = INF;
else rdis = dist(p[dr.first],p[dr.second]); pair<int,int> ans = ldis < rdis ? dl : dr ; //左右两部分的最值点对
ans_dis = min(ldis,rdis); //左右两部分的最值 // 从中向左右两边找在[p[m].x-d,p[m].x+d]的平面内所有点
// 这以后的复杂度就不太好估计了...
// 这段模板是用暴力找的...我只做了一点点优化...但为什么加剪枝时间还多了这我不太理解囧
int cnt = ;
// for(int i = l; i <= r; i++)
// {
// if((long long)(p[m].x - p[i].x)*(p[m].x - p[i].x) <= ans_dis)
// tmp[cnt++] = p[i];
// }
for(int i = m ; i >= l ; i--){
LL q = (p[m].x - p[i].x) * (p[m].x - p[i].x);
if(p[i].x < p[m].x - q) break;
if(q <= ans_dis){
tmp[cnt++] = p[i];
}
}
for(int i = m+ ; i <= r ; i++){
LL q = (p[m].x - p[i].x) * (p[m].x - p[i].x);
if(p[i].x > p[m].x + q) break;
if(q <= ans_dis){
tmp[cnt++] = p[i];
}
}
//按y方向进行筛选 ,相隔大于d的点可以直接跳过
sort(tmp,tmp+cnt,cmp1);
for(int i = ; i < cnt ; i++){
for(int j = i+ ; j < cnt ; j++){
if((tmp[i].y - tmp[j].y) * (tmp[i].y - tmp[j].y) >= ans_dis)
break;
if(dist(tmp[i],tmp[j]) < ans_dis){
ans_dis = dist(tmp[i],tmp[j]);
ans = make_pair(tmp[i].id,tmp[j].id);
}
}
}
return ans;
} void pre(){
t[].x = bx % cx;
t[].y = by % cy;
t[].pos = ;
for(int i = ; i <= n ; i++) {
t[i].x = (t[i-].x * ax + bx) % cx;
t[i].y = (t[i-].y * ay + by) % cy;
t[i].pos = i;
}
}
int main()
{
//FIN;
int T;
scanf("%d",&T);
while(T--){
scanf("%d%I64d%I64d%I64d%I64d%I64d%I64d",&n,&ax,&bx,&cx,&ay,&by,&cy);
pre();
LL res = ;
while(n){
for(int i = ; i < n ; i++)
p[i] = t[i];
sort(p,p+n,cmp);
for(int i = ; i < n ; i++)
p[i].id = i;
pair<int,int> ans = Closest_Pair(,n-);
int last = max(p[ans.first].pos,p[ans.second].pos);
res += (dist(p[ans.first],p[ans.second]) * (n - last));
n = last ;
}
printf("%I64d\n",res);
}
return ;
}

HDU 4631 Sad Love Story 平面内最近点对的更多相关文章

  1. HDU 1007 Quoit Design 平面内最近点对

    http://acm.hdu.edu.cn/showproblem.php?pid=1007 上半年在人人上看到过这个题,当时就知道用分治但是没有仔细想... 今年多校又出了这个...于是学习了一下平 ...

  2. HDU 4631 Sad Love Story (2013多校3 1011题 平面最近点对+爆搞)

    Sad Love Story Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others ...

  3. hdu 4631 Sad Love Story

    http://acm.hdu.edu.cn/showproblem.php?pid=4631 没想到这道题需要用“平均时间复杂度” 计算   一直没有想到解法  因为不符考虑了最坏情况的理念 方法一: ...

  4. 平面内,线与线 两条线找交点 两条线段的位置关系(相交)判定与交点求解 C#

    个人亲自编写.测试,可以正常使用   道理看原文,这里不多说   网上找到的几篇基本都不能用的   C#代码 bool Equal(float f1, float f2) { return (Math ...

  5. hdu 4630 查询[L,R]区间内任意两个数的最大公约数

    No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  7. poj 1106(半圆围绕圆心旋转能够覆盖平面内最多的点)

    Transmitters Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4955   Accepted: 2624 Desc ...

  8. HDU-4631 Sad Love Story 平面最近点对

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631 数据是随机的,没有极端数据,所以可以分段考虑,最小值是一个单调不增的函数,然后每次分治算平面最近 ...

  9. HDU 2050(折线分割平面)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2050 折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    ...

随机推荐

  1. Project Euler:Problem 58 Spiral primes

    Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length ...

  2. UVa 11849 - CD

    题目:给你两个有序序列(每一个序列中元素不同),求两序列中都出现的元素个数. 分析:简单题. 合并排序合并过程. 设置两个指针.指向两序列当前元素.那个元素小指针向后移动.相同大则计数加一,同一时候后 ...

  3. C语言:具体解释指针

    指针应该算得上是c语言的精华,但也是难点. 非常多教程或者博客都有对其具体的解说与分析. 我这一节的内容,也是解说指针.但我会尽量使用图解的方式,使大家非常easy理解及掌握. 一.基本使用 先来看看 ...

  4. ALTERA器件中复位电路实现之-异步复位同步化

    所谓异步复位同步化,就是我们通常说的异步复位同步撤除. 为了避免纯粹的同步复位和纯粹异步复位的问题,可以使用一种叫做同步化的异步复位,我们称其为第三类复位.这种复位完全结合了异步复位和同步复位的优势, ...

  5. linux 不常用命令及命令组合

    lsof:list open files, sudo lsof | grep deleted:则列出虽然被删除,但还处于打开状态的文件.注意,这些文件占用的空间,只有在这些文件关闭时,才会被释放. m ...

  6. C# double保留四位小数

    2.保留N位,四舍五入 . decimal d= decimal.Round(decimal.Parse("0.55555"),4); 3.保留N位四舍五入 Math.Round( ...

  7. 关于servlet的web.xml映射

    1.原理 <servlet> <!-- servlet的名字,随便起个名,但和下面的servlet名一致 --> <servlet-name>hello</s ...

  8. caioj 1063 动态规划入门(一维一边推1:美元和马克)

    这道题一开始我是这么想的 最后的答案肯定是某次的马克换回来的,但这个该怎么确定?? 实际上应该把范围缩小,只看最后一次和倒数第二次之间有什么联系. 可以发现,只有两种可能,最后一天换或者不换.换的话就 ...

  9. 【Uva 10285】Longest Run on a Snowboard

    [Link]: [Description] 在一个r*c的格子上; 求最长的下降路径; [Solution] 记忆化搜索; f[x][y]表示从(x,y)这个格子往下还能走多远; 因为是严格递增,所以 ...

  10. 洛谷——P1155 双栈排序

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...