Sad Love Story(hdu4631,模拟+set容器)

41 篇文章 0 订阅
12 篇文章 0 订阅

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

Sad Love Story

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)

Total Submission(s): 758    Accepted Submission(s): 222

Problem Description

There's a really sad story.It could be about love or about money.But love will vanish and money will be corroded.These points will last forever.So this time it is about points on a plane.

We have a plane that has no points at the start.

And at the time i,we add point pi(xi, yi).There is n points in total.

Every time after we add a point,we should output the square of the distance between the closest pair on the plane if there's more than one point on the plane.

As there is still some love in the problem setter's heart.The data of this problem is randomly generated.

To generate a sequence x1, x2, ..., xn,we let x0 = 0,and give you 3 parameters:A,B,C. Then xi = (xi-1 * A + B) mod C.

The parameters are chosen randomly.

To avoid large output,you simply need output the sum of all answer in one line.

Input

The first line contains integer T.denoting the number of the test cases.

Then each T line contains 7 integers:n Ax Bx Cx Ay By Cy.

Ax,Bx,Cx is the given parameters for x1, ..., xn.

Ay,By,Cy is the given parameters for y1, ..., yn.

T <= 10. 

n <= 5 * 105.

104 <= A,B,C <= 106.

Output

For each test cases,print the answer in a line.

Sample Input

2

5 765934 377744 216263 391530 669701 475509

5 349753 887257 417257 158120 699712 268352

Sample Output

8237503125

49959926940

Hint

If there are two points coincide,then the distance between the closest pair is simply 0.

 

 解析:

 题意:

 在一平面上逐次加入点,计算每次两点建最短距离的平方的和

 思路:

运用mulitset

模拟

 这里利用了mulitset特点:可插入重复的值,自动按照定义的从小到大排序

 所以每插入一个点进行,由这个点向左右方向查找,查找过程要注意剪枝

10734MS 23776K 1159 B C++

*/


#include<stdio.h>
#include<string.h>
#include<set>
#include<algorithm>
#include<iostream>
using namespace std;
long long  min(long long a,long long b){return a<b? a:b;}
struct point
{
long long x;
long long y;
bool operator<(const point &a)const
{
return x<a.x;
}
};
int main()
{
int T,n,i;
long long  ax,bx,cx,ay,by,cy;
long long ans,tx,ty,t;
scanf("%d",&T);
while(T--)
{   multiset<point>p;//运用multiset容器的优点,可插入重复的值,自动按照定义的从小到大排序
scanf("%d",&n);
   scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&ax,&bx,&cx,&ay,&by,&cy);
   tx=ty=0;
   ans=0;
  t=1000000000000;
  long long dx,dy;
for(i=0;i<n;i++)
{tx=(tx*ax+bx)%cx;
 ty=(ty*ay+by)%cy;
 point pt;
 pt.x=tx,pt.y=ty;
  if(i>0)
  {
  	multiset<point>::iterator cur=p.lower_bound(pt),it;//cur是返回第一个比pt大的值得定位器
  	for(it=cur;it!=p.end();it++)//往右找
  	{dx=it->x-pt.x;
  	 dy=it->y-pt.y;
  	 if(dx*dx>=t)//由于元素是按照x轴从小到大的顺序排列,很显然地此时已经没有再继续比较下去的必要
  	 break;
  	 t=min(t,dx*dx+dy*dy);
  	}
  	for(it=cur;it!=p.begin();)//往左找
  	{--it;
 dx=it->x-pt.x;
  	 dy=it->y-pt.y;
  	 if(dx*dx>=t)
  	 break;
  	 t=min(t,dx*dx+dy*dy);
  	}
  	ans+=t;
  }
 p.insert(pt);
}
printf("%I64d\n",ans);
}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值