【Codeforces 429D】 Tricky Function
【题目链接】
http://codeforces.com/problemset/problem/429/D
【算法】
令Si = A1 + A2 + ... + Ai(A的前缀和)
则g(i,j) = Sj - Si
f(i,j) = (i-j)^2 + (Si - Sj)^2
观察这个式子,我们发现可以用类似于平面最近点对的算法来求解该问题
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
const long long INF = 1e15; int i,n,x;
long long sum[MAXN]; struct point
{
long long x,y;
} a[MAXN]; inline bool cmpx(point a,point b)
{
return a.x < b.x;
}
inline bool cmpy(point a,point b)
{
return a.y < b.y;
}
inline long long dist(point a,point b)
{
return abs(a.x - b.x) * abs(a.x - b.x) + abs(a.y - b.y) * abs(a.y - b.y);
}
inline long long Closest_Pair(int l,int r)
{
int i,j,mid,len = ;
long long d;
static point s[MAXN];
if (l == r) return INF;
if (l + == r) return dist(a[l],a[r]);
mid = (l + r) >> ;
d = min(Closest_Pair(l,mid),Closest_Pair(mid+,r));
for (i = l; i <= r; i++)
{
if ((double)abs(a[i].x - a[mid].x) <= (double)sqrt(d)) s[++len] = a[i];
}
sort(s+,s+len+,cmpy);
for (i = ; i <= len; i++)
{
for (j = i + ; j <= len && s[j].y - s[i].y <= sqrt(d); j++)
{
d = min(d,dist(s[i],s[j]));
}
}
return d;
} int main()
{ scanf("%d",&n);
for (i = ; i <= n; i++)
{
scanf("%d",&x);
sum[i] = sum[i-] + x;
}
for (i = ; i <= n; i++) a[i] = (point){i,sum[i]};
sort(a+,a+n+,cmpx);
printf("%I64d\n",Closest_Pair(,n)); return ; }
【Codeforces 429D】 Tricky Function的更多相关文章
- 【codeforces 429D】Tricky Function
[题目链接]:http://codeforces.com/problemset/problem/429/D [题意] 给你n个数字; 让你求出一段区间[l,r] 使得 (r−l)2+(∑rl+1a[i ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 604D】Moodular Arithmetic
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 602D】Lipshitz Sequence
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- Mybatis学习总结一
一.Mybatis架构 JAR包下载地址 1. mybatis配置 SqlMapConfig.xml,此文件作为mybatis的全局配置文件,配置了mybatis的运行环境等信息. mapper. ...
- 01C++编辑编译运行环境
C++编辑编译运行环境 Bloodshed Dev-C++ Microsoft Visual Studio
- 09C语言指针
C语言指针 地址 地址就是数据元素在内存中的位置表示: &变量名 #include <stdio.h> int main(){ int aa; unsigned int bb = ...
- 10Oracle Database 数据表数据查询
Oracle Database 数据表数据查询 DML 数据操纵语言 - 数据的查看和维护 select / insert /delete /update 基本查询语句 Select [distinc ...
- svn无法显示日期和作者
当遇到这种情况,只要把这个read改为none就可以显示了 亲测绝对管用
- 最大子段和(洛谷P1115,动态规划递推)
洛谷题目链接 题目赋值出来格式有问题,所以我就只放题目链接了 下面为ac代码 #include<bits/stdc++.h> #define ll long long using name ...
- Gym - 101670C Chessboard Dancing(CTU Open Contest 2017 找规律)
题目:链接 思路: 多画出几个情况就可以找出规律来了 Knight (当大于2的时候只要两种颜色相间出现就可以了) King(当大于等于3的时候,总可以用四种形式来补色,具体如下) Bishop(斜 ...
- Ztree加载完成默认选中根节点右侧生成表格
需求:页面加载完成之后,默认选中ztree的根节点,并执行其点击方法,右侧生成表格: 效果:如下图所示: 思路:在节点点击事件clickNode方法中根据节点的部门code查询这个部门下的所有员工,并 ...
- python_ 学习笔记(运算符)
python的运算符基本和C语言一致,以下说一些不一样的! 算术运算符 **:代表乘方,对应也有**=: //:代表商向下取整,对应也有//=: 逻辑运算符 and or not 位运算符 :& ...
- 10 Minutes to pandas中文版
本文是对pandas官方网站上<10 Minutes to pandas>的一个简单的翻译,原文在这里.这篇文章是对pandas的一个简单的介绍,详细的介绍请参考:Cookbook .习惯 ...