hdu 4631 Sad Love Story (STL+multiset)

Sad Love Story

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1710    Accepted Submission(s): 548


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 p i(x i, y i).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 x 1, x 2, ..., x n,we let x 0 = 0,and give you 3 parameters:A,B,C. Then x i = (x i-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 A x B x C x A y B y C y.
A x,B x,C x is the given parameters for x 1, ..., x n.
A y,B y,C y is the given parameters for y 1, ..., y n.
T <= 10.
n <= 5 * 10 5.
10 4 <= A,B,C <= 10 6.
 

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.
 

题意: 给你坐标的计算公式xi=(xi-1*A+B)%C(y计算相同,但A、B、C值的不同),然后可以得

到n个坐标,在平面上添加坐标,每添加一个坐标则计算平面上2坐标间的最小值,然后要求

输出所有最小值的和。

思路:按照题目要求,按1-n的顺序添加点。在已有的点集中按X坐标从小到大排序,每增加一

个点,找到大于等于它的位置t,分为两部分,然后从右递增计算要增加的点与点集中的点的距

离,若>=Min,则退出,然后再从t-1处从右往左递减计算要增加的点与点集中所有点的距离的最

小值。

 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#define LL long long
using namespace std;
const LL inf=((LL)1<<62);
const int maxn=500010;

struct node
{
    LL x,y;
} a[maxn],A,B,C;
int n,T;
bool operator < (node p,node q)
{
    return p.x<q.x;
}

void input()
{
    scanf("%d",&n);
    scanf("%I64d %I64d %I64d",&A.x,&B.x,&C.x);
    scanf("%I64d %I64d %I64d",&A.y,&B.y,&C.y);
    a[0].x=a[0].y=0;
    for(int i=1; i<=n; i++)
    {
        a[i].x=(a[i-1].x*A.x+B.x)%C.x;
        a[i].y=(a[i-1].y*A.y+B.y)%C.y;
    }
}

void solve()
{
    multiset <node> st;
    multiset <node>::iterator it,t;
    st.insert(a[1]);
    LL ans=0,Min=inf;
    for(int i=2; i<=n; i++)
    {
        node tmp=a[i];
        t=st.lower_bound(tmp);
        for(it=t;it!=st.begin();)
        {
            it--;
            LL xx=it->x-tmp.x;
            xx=xx*xx;
            if(xx>=Min)   break;
            Min=min(Min,xx+(it->y-tmp.y)*(it->y-tmp.y));
        }
        for(it=t;it!=st.end();it++)
        {
            LL xx=it->x-tmp.x;
            xx=xx*xx;
            if(xx>=Min)   break;
            Min=min(Min,xx+(it->y-tmp.y)*(it->y-tmp.y));
        }
        st.insert(a[i]);
        ans+=Min;
    }
    printf("%I64d\n",ans);
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        input();
        solve();
    }
    return 0;
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值