题目链接  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(平面最近点对)的更多相关文章

  1. Codeforces(429D - Tricky Function)近期点对问题

    D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Codeforces 429D Tricky Function 近期点对

    题目链接:点击打开链接 暴力出奇迹. 正解应该是近期点对.以i点为x轴,sum[i](前缀和)为y轴,求随意两点间的距离. 先来个科学的暴力代码: #include<stdio.h> #i ...

  3. Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对

    D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...

  4. codeforce 429D. Tricky Function (思维暴力过)

    题目描述 Iahub and Sorin are the best competitive programmers in their town. However, they can't both qu ...

  5. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  6. 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点

    平面最近点对,即平面中距离最近的两点 分治算法: int SOLVE(int left,int right)//求解点集中区间[left,right]中的最近点对 { double ans; //an ...

  7. Codefoces 429 D. Tricky Function

    裸的近期点对.... D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  8. HDU-4631 Sad Love Story 平面最近点对

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631 数据是随机的,没有极端数据,所以可以分段考虑,最小值是一个单调不增的函数,然后每次分治算平面最近 ...

  9. HDU1007--Quoit Design(平面最近点对)

    Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...

随机推荐

  1. Androd安全——反编译技术完全解析

    )第二步成功后我们会发现在当前目录下多了一个<APKName>文件夹,这个文件夹中存放的就是反编译的结果了.我们可以打开AndroidManifest.xml.res/layout即可查看 ...

  2. 1022: [SHOI2008]小约翰的游戏John9(Auti_SG)

    1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3150  Solved: 2013[Submit] ...

  3. linux环境搭建系列之jdk安装

    JDK是java软件开发包的简称,要想开发java程序就必须安装JDK.没有JDK的话,无法编译Java程序. 前提: linux centOS6.6 64位操作系统 ROOT账号 安装包的获取:官网 ...

  4. 重新安装Linux自带的JDK

    1.卸载现有jdk 查看本机已经安装的JDK的版本: [root@mcb ~]# java -version java version "1.6.0" OpenJDK Runtim ...

  5. 【LeetCode】Remove Duplicates from Sorted List(删除排序链表中的重复元素)

    这道题是LeetCode里的第83道题. 题目描述: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示例 2: ...

  6. [超级基础]Web安全之SQL注入由浅入深(?)

    前言 断断续续看Web安全到现在了,感觉对很多基础知识还是一知半解,停留在模糊的层次.所以准备系统总结一下. Sql注入我以前一直不以为然,一是现在能sql的站确实很少,二是有像sqlmap的工具可以 ...

  7. [oldboy-django][1初始django]昨日回顾

    1 web框架的本质:socket客户端和服务端通信,socket遵循http协议(短连接,无状态),规定了数据格式(消息头,消息体):传输的数据本质上是字符串 2 django: django安装( ...

  8. js日期处理

    Js获取当前日期时间及其它操作 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整 ...

  9. SVD简化数据

    一,引言 我们知道,在实际生活中,采集到的数据大部分信息都是无用的噪声和冗余信息,那么,我们如何才能剔除掉这些噪声和无用的信息,只保留包含绝大部分重要信息的数据特征呢? 除了上次降到的PCA方法,本次 ...

  10. Struts2 改变语言状态

    只要在请求中增加 request_locale=en_US 参数,就可以实现语言的切换,内部由拦截器实现