AtCoder Grand Contest 004 : Colorful Slimes (DP)

Colorful Slimes

时间限制: 2 Sec  内存限制: 256 MB
提交: 235  解决: 36
[提交][状态][讨论版][命题人:admin]

题目描述

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

输入

The input is given from Standard Input in the following format:

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中颜色,  有两种操作

 一 直接制造  花费时间a[i]秒

二 现拥有的全部转换  i-1 -> i    ,i -> i+1,   i+1 -> i+2   花费时间x 秒


问获得n 种颜色 需要 最少时间;


dp[i][j]      i 代表制作第i 个颜色,  j 代表转换次数;

最优 应该为  k*x + sum( dp[i][k]) 

先dp  每种颜色 最优的策略 ,  然后  计算  k中 转换里  sum 和 最小的。  加上 k*x

因为  是 转换一次, 那么 全部都需要 转换,所有 n 中颜色 实际上 是 一体的。


【code】

#include<bits/stdc++.h>
 
using namespace std;
const int N=2000+10;
typedef long long ll;
const ll llf=(1ll<<63)-1;
ll dp[N][N],a[N];
int main()
{
    int n;
    ll x,ans=llf;
    scanf("%d%lld",&n,&x);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        dp[0][i]=a[i];
    }
    for(int i=1;i<=n-1;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int ind=j-i;
            if(ind<=0)  ind=n+ind;
            dp[i][j]=min(dp[i-1][j],a[ind]);
        }
    }
    for(int i=0;i<=n-1;i++)
    {
        ll ant=0;
        for(int j=1;j<=n;j++)
            ant+=dp[i][j];
        ans=min(ans,ant+i*x);
       // cout<<ans<<endl;
    }
    printf("%lld\n",ans);
    return 0;
}

123 

posted @ 2018-04-11 16:50  Sizaif  阅读(247)  评论(0编辑  收藏  举报