AT - Grand - 004 - B - Colorful Slimes(思维理解)

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 种颜色,最少要花费多少秒?

  • 【操作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<bits/stdc++.h>
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:写题解真是费劲啊….

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值