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. hdoj-1164-Eddy&#39;s research I【分解质因数】

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...

  2. Metasploit - Tips for Evading Anti-Virus

    绕过杀毒软件,有很多钟方法.此处介绍一种,编写python程序调用shellcode,并使用Pyinstaler将python程序编译为exe程序. 准备工作:(Windows XP环境下编译) 将P ...

  3. 多校-HDU 5351 MZL's Border 数学规律

    f[1] = 'b', f[2] = 'a', f[i] = f[i - 1] + f[i - 2] 斐波那契数列的字符串,给你n和m,前m位中,最长的前缀等于后缀的长度是多少.1≤n≤1000, 1 ...

  4. linux RAC 安装失败完全卸载

    1,删除软件安装目录 rm -rf /u01/app 2,删除以下目录内容 rm -rf /tmp/.oracle rm -rf   /tmp/* rm -rf   /tmp/ora* rm -rf ...

  5. UESTC 360 Another LCIS

    Another LCIS Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on UESTC. Original ...

  6. 各消息队列对比,Kafka深度解析,众人推荐,精彩好文!

    http://blog.csdn.net/allthesametome/article/details/47362451

  7. 循环GridControl所有行

    ; i < gridView1.RowCount; i++) { DataRowView row = (DataRowView)gridView1.GetRow(i); } gridView1是 ...

  8. 发送邮件被退回,提示: Helo command rejected: Invalid name 错误

    我自己配置的 postfix + dovecot server, 配置了outlook 后, 相同的账号. 在有的电脑上能收发成功, 在有的电脑上发送邮件就出现退信.提示 Helo command r ...

  9. sed的一些tricks

    1.sed -f xx.sed input_file 可以将一系列操作放在一个xx.sed脚本里执行 ``` #!/bin/sed -f ``` 2.在匹配字符串后面或行尾添加内容 在text后面添加 ...

  10. tomcat 分别在window 和 Linux上配置SSL-安全问题

    公司项目收尾后.通过压力測试后的安全測试.安全測试后中,对于网络传输中数据加密问题存在安全隐患. 须要配置SSL. 简介下SSL协议:SSL或者Secure Socket Layer,是一种同意web ...