Codefoces 429 D. Tricky Function
裸的近期点对....
2 seconds
256 megabytes
standard input
standard output
Iahub and Sorin are the best competitive programmers in their town. However, they can't both qualify to an important contest. The selection will be made with the help of a single problem. Blatnatalag, a friend of Iahub, managed to get hold of the problem before
 the contest. Because he wants to make sure Iahub will be the one qualified, he tells Iahub the following task.
You're given an (1-based) array a with n elements.
 Let's define function f(i, j) (1 ≤ i, j ≤ n) as (i - j)2 + g(i, j)2.
 Function g is calculated by the following pseudo-code:
int g(int i, int j) {
    int sum = 0;
    for (int k = min(i, j) + 1; k <= max(i, j); k = k + 1)
        sum = sum + a[k];
    return sum;
}
Find a value mini ≠ j f(i, j).
Probably by now Iahub already figured out the solution to this problem. Can you?
The first line of input contains a single integer n (2 ≤ n ≤ 100000).
 Next line contains n integers a[1], a[2],
 ..., a[n] ( - 104 ≤ a[i] ≤ 104).
Output a single integer — the value of mini ≠ j f(i, j).
4
1 0 0 -1
1
2
1 -1
2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long int LL; const int maxn=100100;
const LL INF=1LL<<62; LL a[maxn];
int n; struct POINT
{
LL x,y;
}p[maxn],temp[maxn]; bool cmpxy(POINT a,POINT b)
{
if(a.x!=b.x) return a.x<b.x;
return a.y<b.y;
} bool cmpy(POINT a,POINT b)
{
return a.y<b.y;
} inline LL square(LL x)
{
return x*x;
} LL dist(POINT a,POINT b)
{
return square(a.x-b.x)+square(a.y-b.y);
} LL Close_pair(int left,int right)
{
LL d=INF;
if(left==right) return INF;
if(left+1==right) return dist(p[left],p[right]);
int mid=(left+right)/2;
d=min(Close_pair(left,mid),Close_pair(mid+1,right)); int k=0;
for(int i=left;i<=right;i++)
{
if(square(p[i].x-p[mid].x)<=d)
{
temp[k++]=p[i];
}
}
sort(temp,temp+k,cmpy);
for(int i=0;i<k;i++)
{
for(int j=i+1;j<k&&square(temp[i].y-temp[j].y)<d;j++)
{
d=min(d,dist(temp[i],temp[j]));
}
} return d;
} int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%I64d",a+i);
p[i].x=i;
p[i].y=p[i-1].y+a[i];
}
sort(p+1,p+1+n,cmpxy);
printf("%I64d\n",Close_pair(1,n));
return 0;
}
Codefoces 429 D. Tricky Function的更多相关文章
- Codeforces Round #245 (Div. 1) 429D - Tricky Function  最近点对
		D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ... 
- Codeforces 429D Tricky Function(平面最近点对)
		题目链接 Tricky Function $f(i, j) = (i - j)^{2} + (s[i] - s[j])^{2}$ 把$(i, s[i])$塞到平面直角坐标系里,于是转化成了平面最近点 ... 
- Codeforces(429D - Tricky Function)近期点对问题
		D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes input standard ... 
- 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) = ... 
- 【codeforces 429D】Tricky Function
		[题目链接]:http://codeforces.com/problemset/problem/429/D [题意] 给你n个数字; 让你求出一段区间[l,r] 使得 (r−l)2+(∑rl+1a[i ... 
- Codeforces 429D Tricky Function 近期点对
		题目链接:点击打开链接 暴力出奇迹. 正解应该是近期点对.以i点为x轴,sum[i](前缀和)为y轴,求随意两点间的距离. 先来个科学的暴力代码: #include<stdio.h> #i ... 
- javascript零散要点收集
		1.this永远指向函数对象的所有者 2.ECMA-262 把对象(object)定义为“属性的无序集合,每个属性存放一个原始值.对象或函数”.严格来说,这意味着对象是无特定顺序的值的数组. 3.pr ... 
- CF数据结构练习
		1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ... 
随机推荐
- yii中设置提示成功信息,错误提示信息,警告信息
			方法一: <?phpYii::app()->user->setFlash(‘success’,”Data saved!”); 设置键值名为success的临时信息.在getFlash ... 
- Yii与表单交互的三种模式2
			在yii的标签中加入css或js方法:echo $form->textField($model,'starttime',array( 'onclick'=>'alert(&q ... 
- 使用Selenium+appium进行App测试
			1. 启动安卓虚拟机. 
- Loadrunner常用的分析要点都有哪些
			提供了生产负载的虚拟用户运行状态的相关信息,可以帮助我们了解负载生成的结果. Rendezvous(负载过程中集合点下的虚拟用户): 当设置集合点后会生成相关数据,反映了随着时间的推移各个时间点上并发 ... 
- 在Mac OS X 通过抓包、“第三方下载工具”加速下载、安装APP或系统
			#!/bin/bash ######################################################################################## ... 
- python中隐式的内存共享
			在python中,基本上使用的是引用,那么就会造成一个隐式的内存共享,特别是在容器对象中,例如list,dictionary 对于不可变对象,是不会造成隐式的内存共享情况,如下所示: >> ... 
- Mysql字符串截取函数SUBSTRING的用法说明
			感觉上MySQL的字符串函数截取字符,比用程序截取(如PHP或JAVA)来得强大,所以在这里做一个记录,希望对大家有用. 函数: 1.从左开始截取字符串 left(str, length) 说明:le ... 
- 基于AWS的自动化部署实践
			过年前,我给InfoQ写了篇文章详细介绍我们团队在过去4年基于AWS的自动化部署实践.文章包括了:为什么选择AWS.AWS上自动化部署的优势和挑战.我们的解决方案,以及和AWS DevOps方案(Op ... 
- html5 拖拽
			<!doctype html> <html> <head> <meta charset="utf-8"> <title> ... 
- 第一百九十六天 how can I 坚持
			老妈邮的咸菜到了,美味啊,买不到,哈哈. 以后要勤给鱼换水啊,10天不换,水都臭了,拒绝懒惰. 明天要回济南了,刘松结婚,估计又没法发博客了. 两条鱼,一条罗娜,一条我,哈哈. 睡觉. 
