题目大意,给你一个长度为n的数组a,和一个长度为m的b数组,让你求出b数组中长度为n的数组使得SUM(a[i] - b[i])2 最小;
题解:这是一道水题,简单的暴力枚举就好了
接下来是代码
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<queue> #include<vector> #define ll long long using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 100010; int a[maxn]; int b[maxn]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } int m; cin >> m; for (int j = 1; j <= m; j++) { cin >> b[j]; } int minx = inf; for (int j = n; j <= m; j++) { int sum = 0; for (int i = j,k=n; i >= j - n + 1; i--,k--) { sum += pow(b[i] - a[k], 2); } minx = min(sum, minx); } cout << minx << endl; }