B - Colorful Slimes
Time limit : 2sec / Memory limit : 256MB
Score : 400 points
Problem Statement
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
Input
The input is given from Standard Input in the following format:
N x
a1 a2 … aN
Output
Find the minimum time that Snuke needs to have slimes in all N colors.
Sample Input 1
Copy
2 10
1 100
Sample Output 1
Copy
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.
Sample Input 2
Copy
3 10
100 1 100
Sample Output 2
Copy
23
Snuke can act as follows:
Catch a slime in color 2. This takes 1 second.
Cast the spell. The color of the slime changes: 2 → 3. This takes 10 seconds.
Catch a slime in color 2. This takes 1 second.
Cast the soell. The color of each slime changes: 3 → 1, 2 → 3. This takes 10 seconds.
Catch a slime in color 2. This takes 1 second.
Sample Input 3
Copy
4 10
1 2 3 4
Sample Output 3
Copy
10
Snuke can act as follows:
Catch a slime in color 1. This takes 1 second.
Catch a slime in color 2. This takes 2 seconds.
Catch a slime in color 3. This takes 3 seconds.
Catch a slime in color 4. This takes 4 seconds.
题目链接:https://agc004.contest.atcoder.jp/tasks/agc004_b
题意:通过以下两种操作恰好获得 n 种颜色,最少要花费多少秒?
解题思路:
若你想要获得 颜色 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次操作②中,根据每种颜色不同的情况决定在第几次操作②的时候使用操作①)
代码如下
#include
using namespace std;
typedef long long LL;
const int maxn = 2e3 + 5;
LL a[maxn];
LL b[maxn][maxn];//b[i][k] : 当整个过程使用了【k】次魔法,获得【color i】时的最少花费的时间。(不包括【k * x】).
int main(){
int n;
LL x,sum = 1LL << 60LL;
cin >> n >> x;
for(int i = 1;i <= n;i++) cin >> a[i];
for(int i = 1;i <= n;i++){
b[i][0] = a[i];
for(int k = 1;k <= n - 1;k++){
int pos = i - k;
if(pos <= 0) pos = n - abs(pos);
b[i][k] = min(b[i][k - 1],a[pos]);//【b[i][k]】的转移方程.
}
}
for(int k = 0;k <= n - 1;k++){
LL ans = 0;
for(int i = 1;i <= n;i++){
ans += b[i][k];
}
sum = min(sum,ans + x * k);//取最小值.
}
cout << sum << endl;
}
ps:写题解真是费劲啊….