【CodeForces 429D】Tricky Function 【思维转化+分治求最近点对】

37 篇文章 0 订阅
25 篇文章 1 订阅

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 ] )的点对的最近距离,这个我们可以用分治来求。

原文链接

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 100000 + 10;
const int MAXM = 1e6;
const int mod = 1e9+7;
const LL inf = 0x3f3f3f3f3f3f3f;
struct Node{
    LL x,y;
}Point[MAXN],Q[MAXN];
bool cmpx(Node a,Node b) { return a.x<b.x ; }
bool cmpy(Node a,Node b){ return a.y<b.y ; }
LL sqr(LL a){ return a*a;  }
LL GetDis(Node a,Node b){ return sqr(a.x-b.x)+sqr(a.y-b.y); } 
LL DIV(int le,int ri){
    LL ret=inf;
    if(le==ri) return ret;
    if(le+1==ri) return GetDis(Point[le],Point[ri]);
    int mid=(le+ri)>>1;
    LL d1=DIV(le,mid);
    LL d2=DIV(mid+1,ri);
    ret=min(d1,d2);
    double d=sqrt((double)ret);
    int k=0;
    for(int i=mid;i>=le;i--) {
        if(Point[mid].x-Point[i].x>(LL)d) break;
        Q[++k]=Point[i];
    } 
    for(int i=mid+1;i<=ri;i++) {
        if(Point[i].x-Point[mid].x>(LL)d) break;
        Q[++k]=Point[i];
    }
    sort(Q+1,Q+1+k,cmpy);
    for(int i=1;i<k;i++){
        for(int j=i+1;j<=k&&Q[j].y-Q[i].y<=(LL)d;j++)
        ret=min(ret,GetDis(Q[j],Q[i]));
    }
    return ret;
}
int a[MAXN];
int main(){
    CLOSE();
//  fread();
//  fwrite();
    int n;scanf("%d",&n);
    LL sum=0;
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
        sum+=a[i];
        Point[i].x=i;
        Point[i].y=sum;
    }
    sort(Point+1,Point+1+n,cmpx);
    printf("%lld\n",DIV(1,n));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值