hdu 4631 Sad Love Story
http://acm.hdu.edu.cn/showproblem.php?pid=4631
没想到这道题需要用“平均时间复杂度” 计算 一直没有想到解法 因为不符考虑了最坏情况的理念
方法一:
每加一个点 就找x值和它接近的 有可能更新最小距离的点进行判断更新 运行的相当的快 无语
方法二:
每求出所有点的最近点对 假如说是p[i],p[j](i<j) 那么在j之后加上的点不影响最小距离
递归j前面的所有点就可以了
由于自己的 求最近点对算法 写的相当烂 所有跑的相当慢,勉强过
代码1和代码2:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
using namespace std; typedef long long ll;
typedef pair<double,double>ppd;
const double PI = acos(-1.);
const double eps = (1e-9);
const int N=1000010;
vector<int>vt[N];
int main()
{
//freopen("data.in","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
for(int i=0;i<N;++i)
vt[i].clear();
int n;
scanf("%d",&n);
int A1,B1,C1,A2,B2,C2;
cin>>A1>>B1>>C1;
cin>>A2>>B2>>C2;
int x=0,y=0;
ll ans=0;
ll tmp=(ll)(1e13);
for(int i=1;i<=n;++i)
{
x=((ll)A1*x+B1)%C1;
y=((ll)A2*y+B2)%C2;
for(int i=0;x+i<=1000000;++i)
{
ll x1=(ll)i*i;
if(x1>=tmp)
break;
for(unsigned int j=0;j<vt[x+i].size();++j)
tmp=min(tmp,x1+(ll)(y-vt[x+i][j])*(y-vt[x+i][j]));
}
for(int i=-1;x+i>=0;--i)
{
ll x1=(ll)i*i;
if(x1>=tmp)
break;
for(unsigned int j=0;j<vt[x+i].size();++j)
tmp=min(tmp,x1+(ll)(y-vt[x+i][j])*(y-vt[x+i][j]));
}
vt[x].push_back(y);
if(tmp!=(ll)(1e13))
ans+=tmp;
}
cout<<ans<<endl;
}
return 0;
} #include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
using namespace std; typedef long long ll;
typedef pair<int,int>pp;
const double PI = acos(-1.);
const double eps = (1e-9);
const int N=1000010;
pp p[N];
struct node
{
int x,y;
int index;
}a[N],b[N];
ll dist(const node &a,const node &b)
{
return (ll)(a.x-b.x)*(a.x-b.x)
+(ll)(a.y-b.y)*(a.y-b.y);
}
bool cmpx(const node &a,const node &b)
{
return a.x<b.x;
}
bool cmpy(const node &a,const node &b)
{
return a.y<b.y;
}
ll dfs(int l,int r,int &k)
{
if(r-l==1)
{k=max(a[l].index,a[r].index);return dist(a[l],a[r]);}
int m=(l+r)>>1;
int k1,k2;
ll d;
ll d1=dfs(l,m,k1);
ll d2=dfs(m,r,k2);
if(d1<d2) {d=d1;k=k1;}
else {d=d2;k=k2;}
int ln=0,M=(int)(sqrt(1.0*d));
for(int i=m+1;i<=r&&a[i].x-a[m].x<=M;++i)
{b[ln]=a[i];++ln;}
for(int i=m-1;i>=l&&a[m].x-a[i].x<=M;--i)
{b[ln]=a[i];++ln;}
sort(b,b+ln,cmpy);
for(int i=0;i<ln;++i)
for(int j=1;j<=7&&i+j<ln;++j)
if(dist(b[i],b[i+j])<d)
{d=dist(b[i],b[i+j]);k=max(b[i].index,b[i+j].index);}
return d;
}
ll solve(int n)
{
ll ans=0;
int r=n;
while(r>=2)
{
for(int i=1;i<=r;++i)
{a[i].x=p[i].first;a[i].y=p[i].second;a[i].index=i;}
sort(a+1,a+r+1,cmpx);
int k;
ll tmp=dfs(1,r,k);
ans+=tmp*(r-k+1);
r=k-1;
}
return ans;
}
int main()
{
//freopen("data.in","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
int A1,B1,C1,A2,B2,C2;
scanf("%d %d %d",&A1,&B1,&C1);
scanf("%d %d %d",&A2,&B2,&C2);
p[0].first=0,p[0].second=0;
for(int i=1;i<=n;++i)
{
p[i].first=((ll)A1*p[i-1].first+B1)%C1;
p[i].second=((ll)A2*p[i-1].second+B2)%C2;
}
cout<<solve(n)<<endl;
}
return 0;
}
hdu 4631 Sad Love Story的更多相关文章
- HDU 4631 Sad Love Story 平面内最近点对
http://acm.hdu.edu.cn/showproblem.php?pid=4631 题意: 在平面内依次加点,求每次加点后最近点对距离平方的和 因为是找平面最近点对...所以加点以后这个最短 ...
- 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 ...
- hdu 4631(最近点对,容器)
点击打开链接 题意: 给你一个平面,每次加入一个点,当点数>=2时,求最近点对距离的平方,最后输出所有的平方和. 给你a,b,c x[0]=0;x[i]=(x[i-1]*a+b)%c 如果按照平 ...
- 【 2013 Multi-University Training Contest 3 】
HDU 4622 Reincarnation 枚举字符串的起点,构造后缀自动机,每次插入一个字符,就能统计得到当前不同字串的个数,预处理出所有的询问. #include<cstdio> # ...
- hdu 4631Sad Love Story<计算几何>
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631 题意:依次给你n个点,每次求出当前点中的最近点对,输出所有最近点对的和: 思路:按照x排序,然后用s ...
- hdu 1753 大明A+B
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1753 容易出错的事例: 0.1 0.2 1.88 22.22 1 0.01 大概出错的几个点,做久了思维根 ...
- HDU 5828 Rikka with Sequence (线段树)
Rikka with Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...
- HDU-4631 Sad Love Story 平面最近点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631 数据是随机的,没有极端数据,所以可以分段考虑,最小值是一个单调不增的函数,然后每次分治算平面最近 ...
- HDU 4931 Happy Three Friends(水)
HDU 4931 Happy Three Friends 题目链接 题意:6个数字,一个取两个,妹子取三个,问最后谁会赢 思路:排个序,推断前两个和3 - 5个的和谁更大就可以 代码: #includ ...
随机推荐
- 我想有个梦想(I want have a dream)
成东青说过:梦想是什么,梦想就是你坚持就觉得是幸福东西. 多好啊,有梦想,想想就觉得幸福的.也许你会觉得有点做作,但我真觉得是这样.没有梦想就像一个没有了灵魂的躯壳,整天浑浑噩噩,整天麻木的上班下班, ...
- MySQL 定时器EVENT学习
原文:http://blog.csdn.net/lifuxiangcaohui/article/details/6583535 MySQL 定时器EVENT学习 MySQL从5.1开始支持event功 ...
- svn设置提交忽略某些文件或文件夹
在svn客户端,想设置忽略提交.class文件,通过 properties > New > Other 添加一个忽略的属性,,还是不行:部分屏蔽了,部分class还是在列表中 再次参考了一 ...
- python 键值对的树实现
#coding:utf-8 __author__ = 'similarface' class KeyedBinaryTree: def __init__(self):self.tree=EmptyNo ...
- 手把手ssm+idea
https://github.com/judasn/Basic-Multi-Module-SSM https://github.com/liyifeng1994/ssm
- Python开发者须知 —— Bottle框架常见的几个坑
Bottle是一个小巧实用的python框架,整个框架只有一个几十K的文件,但却包含了路径映射.模板.简单的数据库访问等web框架组件,而且语法简单,部署方便,很受python开发者的青睐.Pytho ...
- 图像fft和wavelet变换矩阵和向量区别 dwt2和wavedec2联系
1. 对于小波变换,dwt2 :单级离散2维小波变换 wavedec2 :多级2-D小波分解 matlab中这两者联系是都能对图像进行小波分解,区别是dwt2是二维单尺度小波变换,只能对输入矩阵X一 ...
- windows 下使用 Filezilla server 搭建 ftp 服务器
windows 下使用 Filezilla server 搭建 ftp 服务器 1. Filezilla server 免费,开源, ftp 服务端 2. 下载安装, windows https:/ ...
- 【bzoj1029】道路抢修
[bzoj1029]建筑抢修 传送门 http://www.lydsy.com/JudgeOnline/problem.php?id=1029 分析 http://blog.csdn.net/popo ...
- 【CITE】C# 如何 实现一个窗体和另一个窗体始终保持相对的位置
C# 如何 实现一个窗体和另一个窗体始终保持相对的位置,任由一个窗体移动,当点击按钮时,弹出的另一个窗体也与之保持相对位置(如左上角)你根据第一个窗体的location去算第二个窗体的location ...