点击打开链接

题意:

给你一个平面,每次加入一个点,当点数>=2时,求最近点对距离的平方,最后输出所有的平方和。

给你a,b,c x[0]=0;x[i]=(x[i-1]*a+b)%c

如果按照平常的方法,每次都进行分治法求最近点对,会TLE,如果利用容器,每次都对x从小到大排序,没加入一个点p,找出比p大的第一个数,然后从这个数从两边开始找,如果x*x已经大于ans了,就可以跳出来了。。最后加起来就ok了;

#include"stdio.h"
#include"string.h"
#include"algorithm"
#include"set"
#define N 500005
typedef __int64 LL;
using namespace std;
struct node
{
LL x,y;
bool operator<(const node &a)const
{
return x<a.x;
}
};
LL x[N],y[N],n;
void fun(LL n,LL x[])
{
LL a,b,c;
scanf("%I64d%I64d%I64d",&a,&b,&c);
int i=0;
x[i]=0;
for(i=1;i<=n;i++)
x[i]=(x[i-1]*a+b)%c;
}
LL min(LL a,LL b)
{
return a<b?a:b;
}
void find()
{
LL i;
LL ans;
LL sum;
LL m1,m2;
scanf("%I64d",&n);
fun(n,x);
fun(n,y);
ans=(LL)1<<60;
sum=0;
multiset<node>M;
for(i=1;i<=n;i++)
{
node p;
p.x=x[i];
p.y=y[i];
if(i>1)
{
multiset<node>::iterator it=M.lower_bound(p),e;
//M.lower_bound返回指向大于(或等于)p的第一个元素的迭代器
for(e=it;e!=M.end();e++)
{
m1=e->x-p.x;
if(m1*m1>=ans)break;
m2=e->y-p.y;
ans=min(ans,m1*m1+m2*m2);
}
for(e=it;e!=M.begin();)
{
e--;
m1=e->x-p.x;
if(m1*m1>=ans)break;
m2=e->y-p.y;
ans=min(ans,m1*m1+m2*m2);
}
sum+=ans;
}
M.insert(p);
}
printf("%I64d\n",sum);
M.clear();
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
find();
return 0;
}

hdu 4631(最近点对,容器)的更多相关文章

  1. HDU 4631 Sad Love Story 平面内最近点对

    http://acm.hdu.edu.cn/showproblem.php?pid=4631 题意: 在平面内依次加点,求每次加点后最近点对距离平方的和 因为是找平面最近点对...所以加点以后这个最短 ...

  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. hdu 4277 USACO ORZ (暴力+set容器判重)

    USACO ORZ Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. hdu 1007最近点对问题

    先说下题意,很简单,给n个点的坐标,求距离最近的一对点之间距离的一半.第一行是一个数n表示有n个点,接下来n行是n个点的x坐标和y坐标,实数. 这个题目其实就是求最近点对的距离.主要思想就是分治.先把 ...

  6. HDU 2593 Pirates’ Code (STL容器)

    题目链接 Problem Description Davy Jones has captured another ship and is smiling contently under the sun ...

  7. zoj 2107&&hdu 1007最近点对问题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1107 Quoit Design Time Limit: 5 Seconds   ...

  8. hdu 1007 最近点对问题(Splay解法)

    为什么要写这个题..经典啊,当然,别以为我用分治做的,不过主要思想还是那神奇的六个点共存(一个h*2h的矩形中最多能放下多少个点使得两两距离不超过h) 其实我是在这里看到的 http://commun ...

  9. 【 2013 Multi-University Training Contest 3 】

    HDU 4622 Reincarnation 枚举字符串的起点,构造后缀自动机,每次插入一个字符,就能统计得到当前不同字串的个数,预处理出所有的询问. #include<cstdio> # ...

随机推荐

  1. COJ 2004 序列

    传送门:http://oj.cnuschool.org.cn/oj/home/addSolution.htm?problemID=978 试题描述: WZJ的数字游戏又开始了.他写了N个自然数Ai到黑 ...

  2. Light OJ 1013 Love Calculator(DP)

    题目大意: 给你两个字符串A,B 要求一个最短的字符串C,使得A,B同时为C的子串. 问C最短长度是多少? C有多少种? 题目分析: 做这道题目的时候自己并没有推出来,看了网上的题解. 1.dp[C串 ...

  3. 1B. Spreadsheets

    题目大意: 行和列的两种方式. A是1, B是2,....Z是26, AA是27, AB是28........... 如: BC23代表55列23行 还有一种表示方法:R23C55, 代表23行,55 ...

  4. 【高精度】NCPC 2014 C catalansqure

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1789 题目大意: 求大卡特兰数..公式如下.输入n求Sn(n<=5000) 题目 ...

  5. 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase

    题目链接: http://codeforces.com/problemset/problem/707/D 题目大意: 一个N*M的书架,支持4种操作 1.把(x,y)变为有书. 2.把(x,y)变为没 ...

  6. 【动态规划】Vijos P1143 三取方格数(NOIP2000提高组)

    题目链接: https://vijos.org/p/1143 题目大意: NxN的矩阵,每个值只能取一次,从(1,1)走到(n,n)走三次能取得的最大值. 题目思路: [动态规划] f[x1][y1] ...

  7. Jump Game —— LeetCode

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. QQ互联 回调地址

    http://wiki.connect.qq.com/%E5%9B%9E%E8%B0%83%E5%9C%B0%E5%9D%80%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98% ...

  9. 动态规划——概率dp

    所谓概率dp,用动态规划的思想找到一个事件中可能发生的所有情况,然后找到符合要求的那些情况数,除以总数便可以得到符合要求的事件发生的概率.其核心思想还是通过dp来得到事件发生的所有情况,很类似在背包专 ...

  10. poj 1631 LIS

    题目链接:http://poj.org/problem?id=1631 #include <cstdio> #include <cstring> #include <io ...