Codeforces 429D Tricky Function(平面最近点对)
题目链接 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; }
Codeforces 429D Tricky Function(平面最近点对)的更多相关文章
- Codeforces(429D - Tricky Function)近期点对问题
D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 429D Tricky Function 近期点对
题目链接:点击打开链接 暴力出奇迹. 正解应该是近期点对.以i点为x轴,sum[i](前缀和)为y轴,求随意两点间的距离. 先来个科学的暴力代码: #include<stdio.h> #i ...
- Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对
D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...
- codeforce 429D. Tricky Function (思维暴力过)
题目描述 Iahub and Sorin are the best competitive programmers in their town. However, they can't both qu ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点
平面最近点对,即平面中距离最近的两点 分治算法: int SOLVE(int left,int right)//求解点集中区间[left,right]中的最近点对 { double ans; //an ...
- Codefoces 429 D. Tricky Function
裸的近期点对.... D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- HDU-4631 Sad Love Story 平面最近点对
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631 数据是随机的,没有极端数据,所以可以分段考虑,最小值是一个单调不增的函数,然后每次分治算平面最近 ...
- HDU1007--Quoit Design(平面最近点对)
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
随机推荐
- IDEA配置好maven后新建maven项目一直build失败的解决方法
maven配置了aliyun中央仓库后,IDEA新建maven项目一直出现以下问题: 相信有遇到这个问题的小伙伴很蛋疼,明明maven配置没错,新建项目却一直build失败,为了这个问题我重装过几次I ...
- 某面试公司出的面试题---用JS比较两个版本号高低
一天中午某个公司给我反馈的面试题,说,比较两个文件的版本号,然后我给发过去了,说我的代码不符合他的要求,o(╯□╰)o了var compareVersion = compareVersion||fun ...
- WordCount by Java
WordCount by Java 软测第二周作业 该项目github地址如下: https://github.com/YuQiao0303/WordCount 一.概述 项目WordCount的需求 ...
- python - 接口自动化测试 - RunTest - 测试用例加载执行/测试报告生成
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: run_test.py @ide: PyCharm Com ...
- 微信小程序--列表渲染
HTML: <view class="content" wx:for="{{oneList}}" wx:key = "id" bind ...
- 史林枫:C#.NET利用ffmpeg操作视频实战(格式转换,加水印 一步到位)
ffmpeg.exe是大名鼎鼎的视频处理软件,以命令行参数形式运行.网上也有很多关于ffmpeg的资料介绍.但是在用C#做实际开发时,却遇到了几个问题及注意事项,比如如何无损处理视频?如何在转换格式的 ...
- mysql 修改密码 开启远程访问权限
修改密码 update user set password=password('') where user='root'; FLUSH PRIVILEGES; 远程访问权限: GRANT ALL ...
- 【bzoj2836】魔法树 树链剖分+线段树
题目描述 输入 输出 样例输入 4 0 1 1 2 2 3 4 Add 1 3 1 Query 0 Query 1 Query 2 样例输出 3 3 2 题解 树剖+线段树模板题,不过为什么写的人这么 ...
- Pointcut is not well-formed: expecting 'identifier' at character position 0 ^ || Pointcut is not well-formed: expecting ')' at character position 11 ^
错误提示: 解决方法1:指定execution 在执行目标方法之前指定execution 解决方法2:可能是execution写错了.请仔细检查. 其他——execution参数设置(带问好的可以不配 ...
- PIC单片机之时钟设置
PIC单片机之时钟设置 http://blog.csdn.net/superanters/article/details/8541650 内部时钟和外部时钟? PIC单片机有许多型号可以设置成 用外部 ...