CF Round#782 C - Line Empire
C - Line Empire
贪心
-
每次征服一个国家之后,如果要将首都迁过来,显然之后迁不如现在迁
-
在征服第 i 个国家后,可利用前缀和 \(O(1)\) 算出 迁或不迁 时征服之后国家的代价,若迁的代价小就迁,否则就不迁
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll n, a, b;
ll c[N], s[N];
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T--)
{
cin >> n >> a >> b;
for (int i = 1; i <= n; i++)
{
cin >> c[i];
s[i] = s[i-1] + c[i];
}
int now = 0;
ll ans = 0;
for (int i = 1; i <= n; i++)
{
ans += b * (c[i] - c[now]);
int cnt = n - i;
ll cost1 = a * (c[i] - c[now]) + b * (s[n] - s[i] - cnt * c[i]);
ll cost2 = b * (s[n] - s[i] - cnt * c[now]);
if (cost1 <= cost2)
{
ans += a * (c[i] - c[now]);
now = i;
}
}
cout << ans << endl;
}
return 0;
}