Codeforces 429D Tricky Function 最近点对

题目链接:点击打开链接

暴力出奇迹。

正解应该是最近点对,以i点为x轴,sum[i](前缀和)为y轴,求任意两点间的距离。

先来个科学的暴力代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
#define N 100050
#define ll __int64
ll a[N], sum[N];
ll n;
int main(){
	ll u, v, i, j, que;
	sum[0] = 0;
	while(~scanf("%I64d",&n)){
		for(i=1;i<=n;i++)scanf("%I64d",&a[i]),sum[i] = sum[i-1]+a[i];
		ll ans = a[2]*a[2]+1;
		for(i = 1; i <= n; i++){
			if(i*i>=ans)break;
			ll tmp = ans;
			for(j = i+1; j<=n; j++){
				tmp = min(tmp, (sum[j]-sum[j-i])*(sum[j]-sum[j-i]));
			}
			ans = min(ans, tmp+i*i);
		}
		printf("%I64d\n",ans);
	}
	return 0;
}

Codeforces 429D Tricky Function 最近点对,布布扣,bubuko.com

时间: 2024-10-10 07:51:48

Codeforces 429D Tricky Function 最近点对的相关文章

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

题目链接  Tricky Function $f(i, j) = (i - j)^{2} + (s[i] - s[j])^{2}$ 把$(i, s[i])$塞到平面直角坐标系里,于是转化成了平面最近点对问题. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b); ++i) #define dec(i, a, b) for (int i(a); i >=

Codeforces 429D Tricky Function 近期点对

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

codeforces 429D Tricky Function

这道就是相当于i表示x轴,sum[i]表示y轴,sum[i]表示前i个数的和 那么计算最小两点之间的距离 所谓的就是看了题解恍然大悟的题,就是算的时候先算的j->i的距离, #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; typedef long long ll; #define N 11111 ll a

Codeforces Round #245 (Div. 1)——Tricky Function

l and dished out an assist in the Blackhawks' 5-3 win over the Nashville Predators.Shaw said just playing with the Blackhawks was enough motivation for him."Positive, I'm playing in the NHL," Shaw said after Sunday's win. "What can't you be

【Codeforces 429D】 Tricky Function

[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = Sj - Si f(i,j) = (i-j)^2 + (Si - Sj)^2 观察这个式子,我们发现可以用类似于平面最近点对的算法来求解该问题 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 100010 const

CodeForces 1098F. Ж-function

题目简述:给定字符串$s[1 \dots n](n \leq 2 \times 10^5)$,以及$Q \leq 2 \times 10^5$个询问,每个询问有两个参数$1 \leq l \leq r \leq n$,求 $$ \sum_{i=l}^r \operatorname{lcp}(s[l \dots r], s[i \dots r]), $$ 其中$\operatorname{lcp}(s, t)$表示字符串$s$和$t$的最长公共前缀(Longest Common Prefix)的长

codeforces #30E Tricky and Clever Password KMP+Manacher+二分

题目大意:给定一个字符串S,要求分成A+prefix+B+middle+C+suffix6段,满足: |prefix|=|suffix| |middle|为奇数 prefix+middle+suffix为回文串 除middle外所有段长度都可以为0 要求最大化|prefix|+|middle|+|suffix|,输出一组方案(|prefix|=|suffix|=0时只输出middle) 首先我们发现suffix串是顶着右端点的,因此我们可以枚举|suffix| 对于每个|suffix|我们需要求

CodeForces 598A Tricky Sum

水题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; long long b[32]; void f() { b[0]=1; for(int i=1;i<=30;i++) b[i]=2*b[i-1]; } int main() { f(); int T; scanf(&q

@codeforces - [email&#160;protected] Function

目录 @[email protected] @[email protected] @accepted [email protected] @[email protected] @[email protected] 已知 a 序列,并给定以下关系: \[\begin{cases} f(1, j) = a_j & (1 \le j \le n) \f(i, j) = \min\{f(i - 1, j), f(i - 1, j - 1)\} + a_j & (2 \le i \le j \le