题目传送门
算法
(暴力枚举) $O(n)$
算出总和之后枚举$p_2$的所有可能位置
C++ 代码
#include <iostream>
using namespace std;
using LL = long long; //一定要开long long
const int N = 100010;
int n;
int a[N];
int m, p1, s1, s2, p2;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++ i )
scanf("%d", &a[i]);
scanf("%d%d%d%d", &m, &p1, &s1, &s2);
LL d = 0, t = 0;
for (int i = 1; i < m; ++ i )
d += a[i] * (m - i);
for (int i = m + 1; i <= n; ++ i )
t += a[i] * (i - m);
if (p1 < m)
d += s1 * (m - p1);
else if (p1 > m)
t += s1 * (p1 - m);
LL mi = 0x7fffffffffffffff;
for (int i = 1; i <= n; ++ i )
{
int l = abs(i - m);
if (i < m)
{
if (abs(d + s2 * l - t) < mi)
{
mi = abs(d + s2 * l - t);
p2 = i;
}
}
else if (i > m)
{
if (abs(t + s2 * l - d) < mi)
{
mi = abs(t + s2 * l - d);
p2 = i;
}
}
else
{
if (abs(t - d) < mi)
{
mi = abs(t - d);
p2 = i;
}
}
}
printf("%d", p2);
return 0;
}