【codeforces 429D】Tricky Function
【题目链接】:http://codeforces.com/problemset/problem/429/D
【题意】
给你n个数字;
让你求出一段区间[l,r]
使得
(r−l)2+(∑rl+1a[i])2最小
【题解】
求出前缀和数组sum[i];
可以发现,如果把数组的下标i作为第一维坐标(x),前缀和sum[i]作为第二维坐标(y);
所求的式子就是任意两点之间的距离平方;
问题转化成:已知平面上的n个点;
求最近的两个点之间的距离的平方;
这个可以用分治的方法搞出来;
(感觉就是个剪枝的暴力);
据说复杂度是N⋅log2N
【Number Of WA】
2
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;
const LL INF = 8e18 + 1;
//4e18
//8e18
struct abc{
LL x,y;
};
LL a[N],sum[N];
abc b[N],c[N];
int n;
LL sqr(LL x){
return x*x;
}
LL dis(abc a,abc b){
LL temp = 0;
temp += sqr(a.x-b.x);
temp += sqr(a.y-b.y);
return temp;
}
LL query(int l,int r){
LL ret = INF;
if (l>=r) return ret;
if (l+1==r) return dis(b[l],b[r]);
int m = (l+r)>>1,k = 0;
LL t1 = query(l,m),t2 = query(m+1,r),temp;
ret = min(t1,t2);
rep2(i,m,l){
temp = sqr(b[i].x-b[m].x);
if (temp>ret) break;
c[++k] = b[i];
}
rep1(i,m+1,r){
temp = sqr(b[i].x-b[m].x);
if (temp>ret) break;
c[++k] = b[i];
}
sort(c+1,c+1+k,[&] (abc a,abc b) {return a.y<b.y;});
rep1(i,1,k)
rep1(j,i+1,k){
temp = sqr(c[j].y-c[i].y);
if (temp > ret) break;
ret = min(ret,dis(c[i],c[j]));
}
return ret;
}
int main(){
//Open();
Close();
cin >> n;
rep1(i,1,n) cin >> a[i];
rep1(i,1,n) sum[i] = sum[i-1] + a[i];
rep1(i,1,n){
b[i].x = i,b[i].y = sum[i];
}
sort(b+1,b+1+n,[&] (abc a,abc b) { return a.x < b.x;});
cout << query(1,n) << endl;
return 0;
}
【codeforces 429D】Tricky Function的更多相关文章
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 604D】Moodular Arithmetic
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 602D】Lipshitz Sequence
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- org.xml.sax.SAXParseException: Failed to read schema document 的原因分析与解决方法
现象: org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema documen t 'http://www.s ...
- 线段树合并(【POI2011】ROT-Tree Rotations)
线段树合并([POI2011]ROT-Tree Rotations) 题意 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有nn个叶子节点,满足这些权值为1-n1-n的一个 ...
- BZOJ 3697/3127 采药人的路径 (点分治)
题目大意: 从前有一棵无向树,树上边权均为$0$或$1$,有一个采药人,他认为如果一条路径上边权为$0$和$1$的边数量相等,那么这条路径阴阳平衡.他想寻找一条合法的采药路径,保证阴阳平衡.然后他发现 ...
- 旋转VR相机不头晕:一个反直觉的发现
旋转VR相机不头晕:一个反直觉的发现 本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/deta ...
- Vue基础知识点
基础知识: vue的生命周期: beforeCreate/created.beforeMount/mounted.beforeUpdate/updated.beforeDestory/destorye ...
- nignx 502错误不能使用/的路径方式 即pathinfo
在server中加入 include enable-php-pathinfo.conf; 引入nginx.conf下的这个文件即可. 如果是tp框架,主要隐藏index.php的入口文件,再加入下面这 ...
- System.arraycopy用法
System.arraycopy用法 注意长度的设置: public class ArrCopy { public static void main(String[] args) { int [] s ...
- 屌丝也能开发安卓版2048(App Inventor)
想编写安卓游戏.java太难.来试试App Inventor.尽管有人觉得他是中学生的玩具,可是也能编写2048这种火爆游戏,不须要太复杂的算法. 整个游戏有几个模块: 一.游戏初始化 数列转化为图形 ...
- 基于One-Class的矩阵分解方法
在矩阵分解中. 有类问题比較常见,即矩阵的元素仅仅有0和1. 相应实际应用中的场景是:用户对新闻的点击情况,对某些物品的购买情况等. 基于graphchi里面的矩阵分解结果不太理想.调研了下相关的文献 ...
- silverlight wpf DataTemplate Command binding
<Grid x:Name="LayoutRoot" Background="White"> <CommonControl:NoapDataGr ...