/*
http://acm.hdu.edu.cn/showproblem.php?pid=4631 Sad Love Story
最短点距
题意:点都是随机的
每加入一个点就求一个最短点距,然后将这些最短点距累加 最后输出
解法:用set维护坐标x的动态有序,然后插入一个点(x,y)
二分查一下它相邻的左边和右边,
然后逐个判断距离是否最小,直到 next_x * next_x > mindis 时break
此时的mindis就是当前的最短点距
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <cmath>
#include <set>
using namespace std;
typedef __int64 ll;
int main(){
freopen("in.txt","r",stdin);
int T;cin >> T;
while(T--){
set < pair<ll, ll> > v;
int n,Ax, Bx, Cx ,Ay, By, Cy;
scanf("%d%d%d%d%d%d%d",&n,&Ax,&Bx,&Cx,&Ay,&By,&Cy);
ll x = 0 , y = 0 , sum = 0 , mdis = (1LL << 60) - 1;
for(int i = 0 ; i < n ; i++){
x = (x * Ax + Bx) % Cx;
y = (y * Ay + By) % Cy;
pair<ll, ll> p(x,y);
if(!v.count(p)){
if(i>0){
set < pair<ll, ll> >::iterator it = v.lower_bound(p) , e;
for(e = it ; e != v.end() ; e++){
int next_x = e->first - p.first;
if(next_x * next_x > mdis)
break;
int next_y = e->second - p.second;
ll dis = (ll)next_x * next_x + (ll)next_y * next_y;
mdis = min(mdis , dis);
}
for(e = it ; e != v.begin() ; ){
e--;
int next_x = e->first - p.first;
if(next_x * next_x > mdis)
break;
int next_y = e->second - p.second;
ll dis = (ll)next_x * next_x + (ll)next_y * next_y;
mdis = min(mdis , dis);
}
sum += mdis;
}
v.insert(make_pair(x,y));
}else{
break;
}
}
//v.clear();
cout << sum << endl;
}
return 0;
}
【解题报告】HDU 4631 Sad Love Story 最短点距(动态)
最新推荐文章于 2021-07-04 15:05:00 发布