题解:CF2107F1 Cycling (Easy Version)
Tendou_Eous · · 题解
我们设
#include<bits/extc++.h>
#define int long long
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = 1e6 + 5;
int n;
int a[maxn],dp[maxn];
void solve()
{
scanf("%lld",&n);
for (int i = 1; i <= n; i++)
scanf("%lld",a + i);
memset(dp,0x3f,sizeof(int) * (n + 5));
dp[0] = 0;// 初始化
for (int i = 1; i <= n; i++)
{
int x = inf,pos = 0;
for (int j = i - 1; ~j; j--)
{
if (a[j + 1] < x)
x = a[j + 1],pos = j + 1;// 求最小值
dp[i] = min(dp[i],dp[j] + i - j - 1 + i - pos + (i - j) * x);// dp 转移
}
}
printf("%lld\n",dp[n]);
}
signed main()
{
int t;
scanf("%lld",&t);
while (t--)
solve();
return 0;
}