D. Tricky Function

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

codeforces.com/problemset/problem/429/D

Description

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?

Input

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

Output a single integer — the value of mini ≠ j  f(i, j).

Sample Input

4
1 0 0 -1

Sample Output

1

HINT

题意

给你n个数,让你求最小的f(i,j)

f(i,j)=(j-i)^2+(sum[j]-sum[i])^2

其中sum表示前缀和

题解:

简单分析一下,俩平方,就是距离嘛

把所有点都变成(i,sum[i])然后就是找最近点对了,然后我们有几种做法:

1.科学的暴力加剪枝

2.最近点对问题

3.对每一个点进行二分  nlogn感觉非常科学

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 2000001
#define mod 10007
#define eps 1e-5
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** struct Point
{
ll x;
ll y;
}point[maxn];
int n;
int tmpt[maxn]; bool cmpxy(const Point& a, const Point& b)
{
if (a.x != b.x)
return a.x < b.x;
return a.y < b.y;
} bool cmpy(const int& a, const int& b)
{
return point[a].y < point[b].y;
} ll dis2(int i, int j)
{
return (point[i].x - point[j].x) * (point[i].x - point[j].x)
+ (point[i].y - point[j].y) * (point[i].y - point[j].y);
} ll sqr(ll x)
{
return x * x;
} ll Closest_Pair(int left, int right)
{
ll d = infll;
if (left == right)
return d;
if (left + == right)
return dis2(left, right);
int mid = (left + right) >> ;
ll d1 = Closest_Pair(left, mid);
ll d2 = Closest_Pair(mid + , right);
d = min(d1, d2);
int i, j, k = ;
//分离出宽度为d的区间
for (i = left; i <= right; i++) {
if (sqr(point[mid].x - point[i].x) <= d)
tmpt[k++] = i;
}
sort(tmpt, tmpt + k, cmpy);
//线性扫描
for (i = ; i < k; i++) {
for (j = i + ; j < k && sqr(point[tmpt[j]].y - point[tmpt[i]].y) < d;
j++) {
ll d3 = dis2(tmpt[i], tmpt[j]);
if (d > d3)
d = d3;
}
}
return d;
} int main()
{
scanf("%d", &n);
ll sum = ;
for (int i = ; i < n; ++i)
{
int x;
scanf("%d", &x);
point[i].x = i;
sum += x;
point[i].y = sum;
}
cout << Closest_Pair(, n - ) << endl;
return ;
}

Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对的更多相关文章

  1. Codeforces Round #277 (Div. 2) A. Calculating Function 水题

    A. Calculating Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/4 ...

  2. Codeforces Round #456 (Div. 2) A. Tricky Alchemy

    传送门:http://codeforces.com/contest/912/problem/A A. Tricky Alchemy time limit per test1 second memory ...

  3. Codeforces Round #245 (Div. 1) B. Working out (简单DP)

    题目链接:http://codeforces.com/problemset/problem/429/B 给你一个矩阵,一个人从(1, 1) ->(n, m),只能向下或者向右: 一个人从(n, ...

  4. Codeforces Round #245 (Div. 1) B. Working out (dp)

    题目:http://codeforces.com/problemset/problem/429/B 第一个人初始位置在(1,1),他必须走到(n,m)只能往下或者往右 第二个人初始位置在(n,1),他 ...

  5. codeforces水题100道 第十题 Codeforces Round #277 (Div. 2) A. Calculating Function (math)

    题目链接:www.codeforces.com/problemset/problem/486/A题意:求表达式f(n)的值.(f(n)的表述见题目)C++代码: #include <iostre ...

  6. Codeforces Round #245 (Div. 1) B. Working out dp

    题目链接: http://codeforces.com/contest/429/problem/B B. Working out time limit per test2 secondsmemory ...

  7. Codeforces Round #245 (Div. 2) C. Xor-tree DFS

    C. Xor-tree Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/C ...

  8. Codeforces Round #245 (Div. 2) B. Balls Game 并查集

    B. Balls Game Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...

  9. Codeforces Round #245 (Div. 2) A. Points and Segments (easy) 贪心

    A. Points and Segments (easy) Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...

随机推荐

  1. Selenium2Library系列 keywords 之 _SelectElementKeywords

    # 公有方法 (1)get_list_items(self, locator)  返回labels集合 _get_select_list_options(self, select_list_or_lo ...

  2. oracle 定时任务(DBMS_JOB)

    示例如下: -- 每隔一分钟执行存储过程p1 create or replace procedure p2 as job_num number; begin dbms_job.submit(job_n ...

  3. JavaScript 建立简单的图片库

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  4. 【Unity入门】场景、游戏物体和组件的概念

    版权声明:本文为博主原创文章,转载请注明出处. 游戏和电影一样,是通过每一个镜头的串联来实现的,而这样的镜头我们称之为“场景”.一个游戏一般包含一个到多个场景,这些场景里面实现了不同的功能,把它们组合 ...

  5. 整理string类常见方法的使用说明

    整理String类的Length().charAt().getChars().replace().toUpperCase().toLowerCase().trim().toCharArray()使用说 ...

  6. Tomcat启动报错:Failed to initialize end point associated with ProtocolHandler ["http-apr-8080"]

    在用MyEclipse做开发,启动Tomcat的时候,控制台老是报错Failed to initialize end point associated with ProtocolHandler [&q ...

  7. linux常用的一些命令(不断增加中)

    linux 下重启 apache: httpd -k restart 下面这些大多命令都可以在<鸟哥私房菜>的服务器中的“常用网络指令”和基础中的“程序与资源管理”中找到ps -aux 这 ...

  8. dom 侧栏分享

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. VS2010 删除空行

    查找内容:^:b*$\n 替换为: 查找范围:当前文档 使用:正则表达式 vs2013 ^\s*(?=\r?$)\n

  10. pyQt 每日一练习 -- 登录框

    #coding=utf-8 #第一个练习,登录框 import sys from PyQt4 import QtGui,QtCore #登录框 class LoginBox(QtGui.QWidget ...