题目链接  Tricky Function

$f(i, j) = (i - j)^{2} + (s[i] - s[j])^{2}$

把$(i, s[i])$塞到平面直角坐标系里,于是转化成了平面最近点对问题。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) typedef long long LL; const int N = 100010; struct Point{
LL x, y;
void scan(){ scanf("%lld%lld", &x, &y);}
} p[N], q[N]; int n;
LL c[N];
LL s; bool cmp_x(const Point &a, const Point &b){
return a.x < b.x;
}
bool cmp_y(const Point &a, const Point &b){
return a.y < b.y;
} LL dist(const Point &a, const Point &b){
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
} LL work(int l, int r){
if (r == l) return 9e18;
if (r - l == 1) return dist(p[l], p[r]);
if (r - l == 2) return min(min(dist(p[l], p[r]), dist(p[l + 1], p[r])), dist(p[l], p[l + 1]));
int mid = (l + r) >> 1, cnt = 0;
LL ret = min(work(l, mid), work(mid + 1, r)); rep(i, l, r) if (p[i].x < p[mid].x + sqrt(ret) && p[i].x > p[mid].x - sqrt(ret)) q[++cnt] = p[i];
sort(q + 1, q + cnt + 1, cmp_y);
rep(i, 1, cnt - 1) rep(j, i + 1, cnt){
if ((q[j].y - q[i].y) * (q[j].y - q[i].y) > ret) break;
ret = min(ret, dist(q[i], q[j]));
} return ret;
} int main(){ scanf("%d", &n);
rep(i, 1, n) scanf("%lld", c + i);
rep(i, 1, n){
s += c[i];
p[i] = {i, s};
}
sort(p + 1, p + n + 1, cmp_x);
printf("%lld\n", work(1, n));
return 0; }

  

最新文章

  1. BZOJ3732 解析报告//LCA,最小生成树
  2. CI中PHP写法规范(不断更新)
  3. ACM题目————还是畅通工程
  4. 数据库操作类util
  5. Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
  6. SDUT 2860-生日Party(BFS)
  7. 机器学习学习-Types of learning
  8. 学习笔记:javascript内置对象:字符串对象
  9. 一文看懂大数据的技术生态Hadoop, hive,spark都有了[转]
  10. 第27月第17天 objc_msgSendSuper
  11. Android屏幕适配全攻略(最权威的官方适配指导)
  12. Rsync同步设置的一例
  13. Scala学习(二)练习
  14. 【Android界面实现】使用PagerTabStrip实现有滑动标签的Viewpager
  15. java 闭包与回调
  16. 使用Dataset构建数据到lgb中
  17. Kiggle:Digit Recognizer
  18. PE框架学习
  19. FusionCharts simple demo for (html+js、APS.NET Webform、MVC)
  20. Cookie和Session入门(一)

热门文章

  1. Java基础知识:Collection接口
  2. heap&amp;stack的区别
  3. C++ STL map容器的说明测试1
  4. 如何将Linux rm命令删除的文件放入垃圾箱
  5. python 学习分享-面向对象2
  6. Leetcode 629.K个逆序对数组
  7. django ORM 外键详解
  8. maven学习(十二)——maven聚合与继承实战
  9. 导入goshop2(复制自己看)
  10. easyui在datagrid只想选择一条