【CodeForces 429D】Tricky Function 【思维转化+分治求最近点对】
来源:互联网 发布:php 设置头部 编辑:程序博客网 时间:2024/06/07 16:06
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;}
- 【CodeForces 429D】Tricky Function 【思维转化+分治求最近点对】
- Codeforces(429D - Tricky Function)最近点对问题
- Codeforces 429D Tricky Function 最近点对
- codeforces 429D Tricky Function
- CodeForces 429D Tricky Function
- 分治策略 求 最近点对问题
- 分治法求最近点对问题
- 分治算法求最近点对
- 分治算法求最近点对
- 分治法求最近点对
- 分治法求最近点对问题
- hdu1007分治法求最近点对
- 分治算法求最近点对
- 分治法求最近点对
- 分治法求最近点对
- 分治法求最近点对
- 分治算法求最近点对
- hdu1007求最近点对(分治法)
- 2017.10.18 當日總結
- 三次翻转解决串逆序
- C语言 常量与变量
- Linux_CentOS 6.5安装Mysql
- 探新Web前端开发(五)
- 【CodeForces 429D】Tricky Function 【思维转化+分治求最近点对】
- 编写“木马生成器”软件总结
- 【数据结构】栈与队列基础
- Spring事务管理@Transactional
- 主机序和网络序
- Rxjava2 Retrofit2 和 mvp再封装
- 第一个数据库联动web应用
- linux初学之sshd远程登陆
- SAP FIORI教程——如何设置Launchpad的多语言文件(SAP Cloud 版)