Colorful Slimes(DP)

Colorful Slimes

时间限制: 2 Sec   内存限制: 256 MB
提交: 252   解决: 44
[ 提交][ 状态][ 讨论版][命题人: 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钟颜色,有1到n的位置,需要将所有的位置涂上颜色,第i个位置每次可以涂第i种颜色,花费是ai,也可以将整体已经涂了的颜色向后移一位,n的位置移到1,花费是x,问将颜色1到n涂满,最小的花费是多少。

此题的关键是移动次数,可以发现,移动次数若是固定的情况下,每个位置得到的ai值也是固定的,所以此题的关键是跑次数,在跑移动次数的同时,贪心得到第j次时,每个位置应该放的最小的用值,最后枚举次数,算出每个位置的和,加上x*j,维护一个最小值。

所以可以设dp[i][j]为第i个位置,移动j个距离时的最小值,移动次数跟当前位置的ai值有关系,即t=i-j。若是t<=0,则t=n+t;

得到状态转移方程:dp[i][j]=min(dp[i][j-1],a[t])

代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
ll Fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n;n=n*n;}return r;}
ll upd(ll x,ll v){x=x+v>=mod?x+v-mod:x+v;return x;}
ll a[maxn],dp[2005][2005];

int main()
{
	ll n,x,i,j,k;
	scanf("%lld%lld",&n,&x);
	for(i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		dp[i][0]=a[i];
	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<n;j++)
		{
			int t=i-j;
			if(t<=0)
			t=n+t;
			dp[i][j]=min(dp[i][j-1],a[t]);
		}
	}
	ll ans=1e18;
	for(i=0;i<n;i++)
	{
		ll sum=0;
		for(j=1;j<=n;j++)
		{
			sum+=dp[j][i];
		}
		ans=min(ans,sum+i*x);
	}
	printf("%lld\n",ans);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值