Codeforces Round #245 (Div. 1)D(最近点对)

D. Tricky Function
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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?

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 test(s)
input
4
1 0 0 -1
output
1
input
2
1 -1
output
2
不难看出 f(i,j)=(j-i)^2+(sum[j]-sum[i])^2 , 于是可以将(i,sum[i])看成平面上的点,则问题转化为了在一个2D平面上求最近点对的距离,用分治法实现:

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         #include 
        
          #include 
         
           #include 
           using namespace std; typedef __int64 ll; const int INF = -1; const int MAXN = 100005; pair 
           
             q[MAXN]; pair 
            
              t1[MAXN]; pair 
             
               t2[MAXN]; ll call(int x1,int y1,int x2,int y2){ return (ll)(x1-x2)*(x1-x2)+(ll)(y1-y2)*(y1-y2); } ll solve(int l,int r){ if(l==r){ return INF; } else if(l+1==r){ if(q[l].second>q[r].second)swap(q[l],q[r]); return call(q[l].first,q[l].second,q[r].first,q[r].second); } int mid=l+r>>1; ll res1=solve(l,mid); ll res2=solve(mid+1,r); int M=(q[mid].first+q[mid+1].first)>>1; ll res; if(res1==-1)res=res2; else if(res2==-1)res=res1; else res=min(res1,res2); int tem=(int)sqrt(res+0.9)+1; int p1=mid; int p2=mid+1; int sz1=0,sz2=0; while(p1>=l && M-q[p1].first 
              
                hehe){ res=hehe; tem=(int)sqrt(res+0.9)+1; } } } return res; } int main() { int n;scanf("%d",&n); for(int i=1;i<=n;i++){ int x;scanf("%d",&x); int y=q[i-1].second+x; q[i]=make_pair(i,y); } ll res=solve(1,n); printf("%I64d\n",res); return 0; } 
               
              
             
            
          
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值