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. Chapter6:函数

    执行函数的第一步是(隐式地)定义并初始化它的形参.所以,函数最外层作用域中的局部变量也不能使用与函数形参一样的名字. 局部静态变量:在程序的执行路径第一次经过对象定义语句时初始化,并且直到程序终止才被 ...

  2. 数往知来C#之接口 值类型与引用类型 静态非静态 异常处理 GC垃圾回收 值类型引用类型内存分配<四>

    C# 基础接口篇 一.多态复习 使用个new来实现,使用virtual与override    -->new隐藏父类方法 根据当前类型,电泳对应的方法(成员)    -->override ...

  3. 设计模式 工厂-Factory

    在开始笔记之前先推荐一个网站:http://design-patterns.readthedocs.org/zh_CN/latest/index.html 网站对每一个Pattern都有详尽的解说.并 ...

  4. 快速入门linux系统的iptables防火墙 1 本机与外界的基本通信管理

    概述 iptables是一种运行在linux下的防火墙组件,下面的介绍可以快速的学习iptables的入门使用. 特点(重要) 它的工作逻辑分为 链.表.规则三层结构. 数据包通过的时候,在对应表中, ...

  5. ipc.Client: Retrying connect to server: h1/192.168.1.61:9000. Already tried 0 time(s);解决方法

    1.检查namenode服务器的是否运行正常,我的问题是没有开启hadoop集群出现的. 2.检查namenode服务器的防火墙是否开放的响应端口,一般内网建议关闭.

  6. Xtrabackup之innobackupex备份恢复详解(转)

    add by zhj:对于Xtrabackup2.2来说,已经解决了本文结尾提到的那个bug,当使用--copy-back时,同时加--force-non-empty-directories 即可.这 ...

  7. USB -- BULK_ONLY和UFI协议

    2 BULK_ONLY和UFI协议 Bulk—Only协议是USB组织针对大容量存储设备制定的一种块存储类协议,目前已经普遍应用于各种移动存储设备. USB设备分为5大类,即显示器.通信设备.音频设备 ...

  8. Windows 2003 服务器安全设置-批处理 (附参考链接)

    长期维护windows服务器终结出来的安全设置批处理与大家分享,复制以下全部内容用记事本另存为bat或者cmd执行 ===================分隔符号=================== ...

  9. Android - 应用名称设置的问题

    今天我想修改我的android应用名称,就是手机桌面上图标下面的名称,根据我的理解我修改AndroidManifest.xml文件中application标签中的android:label=" ...

  10. POJ 3170 Knights of Ni (暴力,双向BFS)

    题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...