题意:给定一个数组你个数的数组a,定义sum(i, j)表示sigma(a[i],...a[j]),以及另外一个函数f(i, j) = (i - j)^2 + sum(i+1, j)^2

求最小的f(i, j)(i < j)

思路:变形一下f(i, j) = (i - j)^2 + (sum[j] - sum[i])^2

那么把i看成x,sum[i]看成y,那就等价于求二维平面的最近点对吗。

二维平面求最近点对有一个经典nlognlogn的分治算法。。

code:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define x first
#define y second
const int Inf = 0x3fffffff;
pair<int, int> p[], tmp[];
int n;
ll dis(int x, int y, int x1, int y1){
ll dx = x - x1, dy = y - y1;
return dx * dx + dy * dy;
} bool cmp(const pii& a, const pii& b){
return a.y < b.y;
} ll X, Y, k;
ll solve(const int l,const int r){
if (l == r) return Inf;
int m = (l + r) >> ;
ll d = solve(l, m), d1 = solve(m+, r);
d = min(d, d1);
k = ;
X = p[m].x;
for (int i = l; i <= r; ++i)
if ((X-p[i].x) * (X-p[i].x) <= d) tmp[k++] = p[i];
sort(tmp, tmp + k, cmp);
for (int i = ; i < k; ++i){
Y = tmp[i].y;
for (int j = i+; j < k; ++j){
if ((Y - tmp[j].y) * (Y - tmp[j].y) > d) break;
d = min(d, dis(tmp[i].x, tmp[i].y, tmp[j].x, tmp[j].y));
}
}
return d;
} void solve(){
p[] = make_pair(, );
int u;
for (int i = ; i <= n; ++i)
scanf("%d", &u), p[i].x = i, p[i].y = p[i-].y + u;
ll ans = solve(, n);
cout << ans << endl;
} int main(){
// freopen("a.in", "r", stdin);
while (scanf("%d", &n) != EOF){
solve();
}
}

codeforces 429D的更多相关文章

  1. ●CodeForces 429D Trick_Function

    题链: http://codeforces.com/problemset/problem/429/D题解: 分治,最近点对 不难发现g(i,j)=sum[j]-sum[i], 那么f(i,j)=(i- ...

  2. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  3. 【codeforces 429D】Tricky Function

    [题目链接]:http://codeforces.com/problemset/problem/429/D [题意] 给你n个数字; 让你求出一段区间[l,r] 使得 (r−l)2+(∑rl+1a[i ...

  4. Codeforces 429D Tricky Function(平面最近点对)

    题目链接  Tricky Function $f(i, j) = (i - j)^{2} + (s[i] - s[j])^{2}$ 把$(i, s[i])$塞到平面直角坐标系里,于是转化成了平面最近点 ...

  5. Codeforces(429D - Tricky Function)近期点对问题

    D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces 429D Tricky Function 近期点对

    题目链接:点击打开链接 暴力出奇迹. 正解应该是近期点对.以i点为x轴,sum[i](前缀和)为y轴,求随意两点间的距离. 先来个科学的暴力代码: #include<stdio.h> #i ...

  7. Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对

    D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...

  8. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  9. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

随机推荐

  1. php CI ip限制

    public function index() { $ip = $this->input->ip_address(); if(!in_array($ip, $this->allowe ...

  2. Excel 日期转换

    public static void importExcel2(File file) throws Exception, IOException { Workbook book = Workbook. ...

  3. Provisional headers are shown,本地测试成功,服务器运行却失败

    基于MVC的项目 具体情况是一个页面在访问的时候进不了首页,但详细页面却可以进去 下面说说解决方法和思路,以便找出问题所在 第一:把服务器代码下载到本地运行,代码是否出错,出错了,问题找到了,没出错接 ...

  4. 使用percona-xtrabackup实现对线上zabbix监控系统数据库mariadb5.5.47的主从同步

    使用percona-xtrabackup实现对线上zabbix监控系统数据库的主从同步 业务背景: zabbix3.0.4是业务的主要监控,部署在一台单机中,为避免数据丢失先对其做数据主从同步,因主数 ...

  5. HttpUtility.UrlEncode

  6. -Dmaven.multiModuleProjectDirectory system propery is not set问题解决

    -Dmaven.multiModuleProjectDirectory system propery is not set问题解决 eclipse中使用maven插件的时候,运行run as mave ...

  7. socket笔记

    参考: http://www.cnblogs.com/dolphinX/p/3460545.html http://www.cnblogs.com/wei2yi/archive/2011/03/23/ ...

  8. jQuery.bind() 函数详解

    bind()函数用于为每个匹配元素的一个或多个事件绑定事件处理函数. 此外,你还可以额外传递给事件处理函数一些所需的数据. 执行bind()时,事件处理函数会绑定到每个匹配元素上.因此你使用bind( ...

  9. CSS()方法设置元素样式

    使用CSS()方法可以直接设置元素的样式,方法为:css(name,value) $(p).css("font-weight", "bold");     // ...

  10. winform-全局异常捕获作用

    using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;using Jxs ...