题目描述
Snuke lives in another world, where slimes are real creatures and kept by some people. Slimes come in N colors. Those colors are conveniently numbered 1 through N. Snuke currently has no slime. His objective is to have slimes of all the colors together.
Snuke can perform the following two actions:
Select a color i (1≤i≤N), such that he does not currently have a slime in color i, and catch a slime in color i. This action takes him ai seconds.
Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color i (1≤i≤N−1) will become color i+1, and the color of a slime in color N will become color 1. This action takes him x seconds.
Find the minimum time that Snuke needs to have slimes in all N colors.
Constraints
2≤N≤2,000
ai are integers.
1≤ai≤109
x is an integer.
1≤x≤109
Snuke can perform the following two actions:
Select a color i (1≤i≤N), such that he does not currently have a slime in color i, and catch a slime in color i. This action takes him ai seconds.
Cast a spell, which changes the color of all the slimes that he currently has. The color of a slime in color i (1≤i≤N−1) will become color i+1, and the color of a slime in color N will become color 1. This action takes him x seconds.
Find the minimum time that Snuke needs to have slimes in all N colors.
Constraints
2≤N≤2,000
ai are integers.
1≤ai≤109
x is an integer.
1≤x≤109
输入
The input is given from Standard Input in the following format:
N x
a1 a2 … aN
N x
a1 a2 … aN
输出
Find the minimum time that Snuke needs to have slimes in all N colors.
样例输入
2 10
1 100
样例输出
12
提示
Snuke can act as follows:
Catch a slime in color 1. This takes 1 second.
Cast the spell. The color of the slime changes: 1 → 2. This takes 10 seconds.
Catch a slime in color 1. This takes 1 second.
题意:通过以下两种操作恰好获得 n 种颜色,最少要花费多少秒?
- 【操作1】:花费 a[i] 秒,直接获得 颜色 i 。
- 【操作2】:使用魔法,花费 x 秒,使得之前获得的 颜色 i 全部变为 颜色 i + 1。(n + 1 = 1)
解题思路:
-
若你想要获得 颜色 i ,一定是先得到 颜色 j (i 可以等于 j) 后再经过使用 【操作2】 k (0 <= k <= n - 1) 次得到 颜色 i 的。
-
假设在恰好获得 n 种颜色的过程中,总共使用 【操作2】 k 次。要获得单个 颜色 i 的话,那么需要获得 颜色 i , i - 1,i - 2….i - k 中的其中一种颜色。
-
比如:当 k = 2 时,要获得 【颜色3】 ,总共会有以下 3 种方法。(下图代表的是对在 整个 过程来说,针对获得 【颜色3】 的过程图,在k次操作②中,根据每种颜色不同的情况决定在第几次操作②的时候使用操作①)
- 因此,若你想获得 【颜色 i】,你最少需要花费 min{ai, ai −1, … , ai − k} 秒(除去使用 【操作2】 的时间)。我们让 b[i][k] 来代表这个值。这样,你要恰好获得 n 种颜色,一共需要花费
秒。
-
b[i][k]可以通过
来求得。
-
复杂度 :
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
long long int a[2010]={0};
long long int b[2010][2010]={0};
int main()
{ long long int n,x,i,j,sum,minx,temp;
scanf("%lld %lld",&n,&x);
for(i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(i=1;i<=n;i++){
b[i][0]=a[i];
for(j=1;j<n;j++){
temp=i-j;
if(temp<=0)
temp=n+temp;
b[i][j]=min(b[i][j-1],a[temp]); //这里是每次都保留最小前面(k-1)中的值 //变换b[i][k]只是代表相对变换k次而言,最小的b[i][k]的值,不一定变k次。
}
}
minx=0x3f3f3f3f3f3f3f3f;
for(i=0;i<n;i++){
sum=0;
for(j=1;j<=n;j++)
sum+=b[j][i];
sum+=i*x;
minx=min(sum,minx);
}
printf("%lld\n",minx);
return 0;
}