【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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- mac install telnet
问题: -bash: telnet: command not found -bash: brew: command not found 解决: /usr/bin/ruby -e "$(cur ...
- xilinx vivado 百度云分享 vivado2019.1 2018.3 2017.4
vivado2019.1 sdx套件 链接:https://pan.baidu.com/s/1ymRpUa2UYTFuafEChA0-ZQ 提取码:cd4p 复制这段内容后打开百度网盘手机App,操作 ...
- Leetcode 498:对角线遍历Diagonal Traverse(python3、java)
对角线遍历 给定一个含有 M x N 个元素的矩阵(M 行,N 列),请以对角线遍历的顺序返回这个矩阵中的所有元素,对角线遍历如下图所示. Given a matrix of M x N elemen ...
- 网络编程 - join及守护线程
一.Join实例(join理解为等待)import threading,timedef run(n): time.sleep(3) print ("task",n)start = ...
- HDU - 2050 - 折线分割平面(数学 + dp)
题意: 我们看到过很多直线分割平面的题目,今天的这个题目稍微有些变化,我们要求的是n条折线分割平面的最大数目.比如,一条折线可以将平面分成两部分,两条折线最多可以将平面分成7部分 思路: 记住结论.. ...
- Re0:DP学习之路 01背包如何打印路径?
伪代码 用二维数组记录,如果出现可以转移的dp那么记录bk[当前体积][装的物品]=1 输出的时候倒推,如果存在连通的边那么输出并且总共的体积减去输出的体积 代码(uva-624,目前wa不明所以,网 ...
- Crossword Answers UVA - 232
题目大意 感觉挺水的一道题.找出左面右面不存在或者是黑色的格子的白各,然后编号输出一横向单词和竖向单词(具体看原题) 解析 ①找出各个格子的编号 ②对每个节点搜索一下 ③输出的时候注意最后一个数据后面 ...
- python环境配置以及基本知识
python---一种解释型语言(脚本语言),具有代码简洁.入门简单.开发效率高的优点.当然不可避免的有着暴露源码.执行效率低的缺点,但毕竟瑕不掩瑜,在数据是无比宝贵的财富的当下,无疑是一门优秀的编成 ...
- springcloud(十三):Ribbon客户端负载均衡实例
一.采用默认的负载均衡策略:RoundRobinRule 轮询策略 1.修改提供者原的控制类 在之前的eureka-client-provider项目的CenterController.java中加入 ...
- 【Codeforces 1129A】Toy Train
[链接] 我是链接,点我呀:) [题意] 火车从1,2,3...n->1的方式绕圈走.(即每次从i走到i+1) 有一些点有货物需要装载,但是每个点只能装上去一个货物. 每个货物都有目标点卸货点( ...