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 ...
随机推荐
- Uva:11401-Triangle Counting
Triangle Counting Time limit1000 ms Description You are given n rods of length 1, 2-, n. You have to ...
- BFS:HDU-1072-Nightmare
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- Altium Designer
抗干扰设计原则: 1.电源线的设计 选择合适的电源 尽量加宽电源线 保证电源线.底线走向和数据传输方向一致 使用抗干扰元器件(磁珠.电源滤波器等) 电源入口添加去耦电容 2.底线的设计 模拟地和数字地 ...
- Apache Compress-使用
Apache Compress 是什么? Apache 提供的文件压缩工具. 运行环境 jdk 1.7 commons-compress 1.15 测试代码 package com.m.basic; ...
- leetcode 【 Reverse Words in a String 】python 实现
题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is b ...
- IntelliJ IDEA下maven Spring MVC配置
1. 导入工程:或者新建Spring web工程,可以参考博客中的Eclipse Spring MVC的方式: 2.配置Tomcat服务器,有两种方式:一是配置maven插件,而是配置本地Tomcat ...
- Composer 下载安装类库
安装 Composer 你需要先下载 composer.phar 可执行文件. curl -sS https://getcomposer.org/installer | php composer.js ...
- java 获取请求的完整url地址
String url=request.getRequestURL()+"?"+request.getQueryString();
- 【转】Apache Thrift - 可伸缩的跨语言服务开发框架
Apache Thrift - 可伸缩的跨语言服务开发框架 Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.本文将从 Java 开发人员角度详 ...
- 【bzoj2280】[Poi2011]Plot 二分+倍增+二分+最小圆覆盖
题目描述 给出一系列点p_1, p_2, ... , p_n,将其分成不多余m个连续的段,第i段内求一个点q_i,使得q_i到这段内点的距离的最大值的最大值最小 输入 第一行,n m下面n行,每行两个 ...