[CF429D] Tricky Function - 平面最近点对

Description

定义函数 \(f(i,j)=(i-j)^2+(\displaystyle\sum_{k=\min(i,j)+1}^{max(i,j)} a_k )^2\)
\(\displaystyle\min_{i\ne j}f(i,j)\)\(abs(a_i) \le 10^4\)

Solution

转化为 2D 平面最近点对问题

考虑到 \(a_i\) 的范围,横坐标的差也不会太大,所以暴力检查 (i,j), \(abs(i,j) \le M\) 即可,其中 \(M\) 为设定的横座标上界

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1000005;
const int M = 2005;
int n, a[N], ans = 1e18;
signed main()
{
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i], a[i] += a[i - 1];
    for (int i = 1; i <= n; i++)
        for (int j = max(1ll, i - M); j < i; j++)
            ans = min(ans, (j - i) * (j - i) + (a[j] - a[i]) * (a[j] - a[i]));
    cout << ans << endl;
}

相关文章:

  • 2022-02-07
  • 2022-02-07
  • 2021-11-09
  • 2022-12-23
  • 2021-09-15
  • 2022-02-07
  • 2022-02-07
猜你喜欢
  • 2022-12-23
  • 2021-12-13
  • 2022-02-07
  • 2021-06-06
  • 2022-02-07
相关资源
相似解决方案