hdu 4631 Sad Love Story(最近点对+暴力)
来源:互联网 发布:专业摩托车导航软件 编辑:程序博客网 时间:2024/05/22 17:25
这题第一次做的时候觉得太暴力了。。不敢搞。。今天看了题解,一共有三种方法可以搞:sol 1是用STL暴力的。。简单粗暴,但是考虑到自己的时间复杂度分析以及对数据的理解不够深,比赛的时候还是慎用STL的好。sol 2是用KD树,那是什么。。。无奈只能尝试一下sol 3了。。先求出所有点的最近点对,然后,对于这对点之后出现的点,累加就行了,然后不断减少上界,直到大于1为止(一个点的时候无距离)。要改一下Closest_Pair的模板,使其返回最近点对的下标然后才能减少上界。。。10+Y,给hdu的long long输入输出跪了,最后怒换cin cout。。。。
#include<algorithm>#include<iostream>#include<cstring>#include<cstdlib>#include<fstream>#include<sstream>#include<bitset>#include<vector>#include<string>#include<cstdio>#include<cmath>#include<stack>#include<queue>#include<stack>#include<map>#include<set>#define FF(i, a, b) for(int i=a; i<b; i++)#define FD(i, a, b) for(int i=a; i>=b; i--)#define REP(i, n) for(int i=0; i<n; i++)#define CLR(a, b) memset(a, b, sizeof(a))#define debug puts("**debug**")#define LL long long#define PB push_back#define eps 1e-10using namespace std;const LL INF = 1LL << 50;const int N = 500005;template <class T> T sqr(T x) { return x*x; }struct Point{ LL x, y; //id1用于求Closest_Pair id2用于求每次找到的上界 int id1, id2; Point(){} Point(LL x, LL y, int id2):x(x), y(y), id2(id2) {}}p[N], point[N], tmpt[N];int n;bool cmpxy(const Point& a, const Point& b){ return a.x < b.x || (a.x == b.x && a.y < b.y);}bool cmpy(const Point& a, const Point& b){ return a.y < b.y;}LL dist(Point a, Point b) //dist函数注意不要sqrt{ return sqr(a.x-b.x) + sqr(a.y-b.y);}struct Ans{ int l, r; Ans(){} Ans(int l, int r):l(l), r(r){}};Ans Closest_Pair(int left, int right){ LL d = INF; //由于函数返回结构体 原先模板中只有1或2个点时统一考虑 if(left==right || left+1 == right) return Ans(left, right); int mid = (left+right)>>1; Ans ans1 = Closest_Pair(left, mid); double d1; if(ans1.l == ans1.r) d1 = INF; //只有一个点的时候无距离 else d1 = dist(point[ans1.l],point[ans1.r]); Ans ans2 = Closest_Pair(mid+1, right); double d2; if(ans2.l == ans2.r) d2 = INF; else d2 = dist(point[ans2.l], point[ans2.r]); Ans ret; if(d1 < d2) ret = ans1; else ret = ans2; d = min(d1,d2); int i,j,k=0; for(i = left; i <= right; i++) { if(sqr(point[mid].x-point[i].x) <= d) //这里也需要改 因为dist函数变了 tmpt[k++] = point[i]; } sort(tmpt,tmpt+k,cmpy); for(i = 0; i < k; i++) { for(j = i+1; j < k && sqr(tmpt[i].y-tmpt[j].y) < d; j++)//这里也是。。。 { double d3 = dist(tmpt[i],tmpt[j]); if(d > d3) { d = d3; ret = Ans(tmpt[i].id1, tmpt[j].id1); } } } return ret;}void read(){ LL a1, b1, c1, a2, b2, c2; cin>>a1>>b1>>c1>>a2>>b2>>c2; p[0] = Point(0, 0, 0); FF(i, 1, n+1) p[i] = Point((p[i-1].x*a1+b1)%c1, (p[i-1].y*a2+b2)%c2, i);}int main(){ int T; scanf("%d", &T); while(T--) { scanf("%d", &n); read(); int end = n; LL ans = 0; while(end > 1) { REP(i, end) point[i] = p[i+1]; sort(point, point+end, cmpxy); REP(i, end) point[i].id1 = i; Ans t = Closest_Pair(0, end-1); int m = max(point[t.l].id2, point[t.r].id2); ans += (end-m+1)*dist(point[t.l], point[t.r]); end = m - 1; } cout<<ans<<endl; } return 0;}
- hdu 4631 Sad Love Story(最近点对+暴力)
- HDU 4631 - Sad Love Story(最近点对)
- hdu 4631 Sad Love Story(暴力)
- hdu 4631 Sad Love Story (暴力set+剪枝)
- hdu 4631 Sad Love Story
- hdu - 4631 - Sad Love Story
- HDU-4631-Sad Love Story
- HDU 4631 Sad Love Story
- hdu 4631Sad Love Story
- 多校第三场 Hdu4631 Sad Love Story(最近点对)
- HDU4631 Sad Love Story(最近点对)转自网络
- hdu 4631 Sad Love Story (STL+multiset)
- HDOJ 4631 Sad Love Story
- hdu 4631 Sad Love Story 多校第三场
- 2013 多校第三场 hdu 4631 Sad Love Story
- 【解题报告】HDU 4631 Sad Love Story 最短点距(动态)
- HDU Sad Love Story StL 暴搜
- Sad Love Story(hdu4631,模拟+set容器)
- 在IIS8添加WCF服务支持
- MFC删除多级目录
- 将session信息写入到memcache中管理
- 解决secureCRT中文乱码问题
- Jquery 创建一个临时层
- hdu 4631 Sad Love Story(最近点对+暴力)
- VirtualBox 使用物理硬盘作为虚拟机硬盘
- 最长等差数列分析
- 手机应用交互设计
- hdu 3038 How Many Answers Are Wrong
- 一个例子说明java数据类型包括对象是赋值还是引用!!!
- 模拟 .net framework 4.0 的 parallel
- Extending maya with C#
- MyEclipse9 安装(Eclipse PHP IDE)PDT插件