cf - 429D
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).
Example
4
1 0 0 -1
1
2
1 -1
2 题目分析 : 首先要对公式进行变形 , 所给的函数就是让求一个前缀和,那么就是由公式的形式就可得到 (i-j)^2 + (sum[i]-sum[j])^2 , 那么不就是转换成一个平面上的最近两点的距离的平方了吗? 代码示例 :
const int eps = 1e5+5;
const double pi = acos(-1.0);
const int inf = 1<<29;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long struct node
{
ll x, y;
}pre[eps], pt[eps]; bool cmpxy(node a, node b){
if (a.x == b.y) return a.y < b.y;
else return a.x < b.x;
} ll dis(ll i, ll j){
return (pre[i].x-pre[j].x)*(pre[i].x-pre[j].x)+(pre[i].y-pre[j].y)*(pre[i].y-pre[j].y);
} ll dis2(ll i, ll j){
return (pt[i].x-pt[j].x)*(pt[i].x-pt[j].x)+(pt[i].y-pt[j].y)*(pt[i].y-pt[j].y);
} bool cmpy(node a, node b){
if (a.y == b.y) return a.x < b.x;
else return a.y < b.y;
} ll close_pair(ll l, ll r){
ll d = 999999999999999;
if (l == r) return d;
if (l + 1 == r) return dis(l, r);
ll m = (l + r) >> 1;
ll d1 = close_pair(l, m);
ll d2 = close_pair(m+1, r);
d = min(d1, d2);
ll k = 0;
for(ll i = l; i <= r; i++){
if ((pre[i].x-pre[m].x)*(pre[i].x-pre[m].x) < d) pt[k++] = pre[i];
}
sort(pt, pt+k, cmpy); for(ll i = 0; i < k; i++){
for(ll j = i+1; j < k && (pt[j].y-pt[i].y)*(pt[j].y-pt[i].y) < d; j++){
ll dd = dis2(i, j);
d = min(dd, d);
}
}
return d;
} int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ll n, x; cin >> n;
ll sum = 0;
for(ll i = 1; i <= n; i++){
scanf("%lld", &x);
sum += x;
pre[i].x = i; pre[i].y = sum;
}
sort(pre+1, pre+1+n, cmpxy);
printf("%lld\n", close_pair(1, n));
return 0;
}
cf - 429D的更多相关文章
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
- ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'
凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- CF memsql Start[c]UP 2.0 A
CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...
- CF memsql Start[c]UP 2.0 B
CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
随机推荐
- vscode编辑如何保存时自动校准eslint规范
在日常开发中,一个大点的项目会有多人参与,那么可能就会出现大家的代码风格不一,各显神通,这个时候就要祭出我们的eslint. 在这之前磨刀不误砍柴工,我们先来配置一下我们的代码编辑工具,如何在vsco ...
- linux 后备缓存
一个设备驱动常常以反复分配许多相同大小的对象而结束. 如果内核已经维护了一套相同 大小对象的内存池, 为什么不增加一些特殊的内存池给这些高容量的对象? 实际上, 内核 确实实现了一个设施来创建这类内存 ...
- C# 将 Begin 和 End 异步方法转 task 异步
在 .NET Framework 有两个不同的异步方法,一个是 Asynchronous Programming Model (APM) 另一个是 Task-based asynchronous pa ...
- 2018.11.30 浪在ACM 集训队第七次测试赛
https://blog.csdn.net/StilllFantasy/article/details/84670643 感谢刘凯同学 https://blog.csdn.net/UnKfrozen/ ...
- vue 插件大全
UI组件 element - 饿了么出品的Vue2的web UI工具套件 Vux - 基于Vue和WeUI的组件库 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开 ...
- 2018-8-10-win10-uwp-按下等待按钮
title author date CreateTime categories win10 uwp 按下等待按钮 lindexi 2018-08-10 19:16:50 +0800 2018-2-13 ...
- HDU3669 Aragorn's Story 树链剖分 点权
HDU3669 Aragorn's Story 树链剖分 点权 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: n个点的,m条边,每个点都 ...
- C# struct和class
文字说明 值类型,存储于堆栈,存在于计算机内存逻辑区域中 引用类型,存储在堆中,位于计算机的不同逻辑位置 struct是值类型 推荐较小的数据类型使用,因为结构的大小会影响应用程序的性能 修改实际上是 ...
- 前端vue——阿里图标的使用方法
阿里图标库的官方网址:https://www.iconfont.cn/ 使用前需要先登录,这里有三种登录方式,本人使用的是新浪微博登录 第一步:找到你需要的图标,点击添加入库 第二步:点击右上角的购物 ...
- git之github解决冲突
1.先创建一个txt文件,并进行编辑 2.然后推送到github,过程看之前的教程. 3.在另一个文件夹拉取(用小乌龟拉取),分别在克隆文件夹和原本文件夹操作test.txt. 4.把本体推送给服务器 ...