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)
题解:比赛的时候理解错题意了,魔法是对于整体来使用的,并不是对单独个体的操作,于是对于n种颜色,可以枚举一个k,这样对于每个元素k*x的花费已经固定了,则只需寻找在k次魔法内可以转换成第i种颜色的最小aj,dp[i][k]代表在k次魔法内能转换成第i种颜色的最小的aj。
#include<stdio.h>
#include <algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
ll a[2005],dp[2005][2005];
int main()
{
ll n,x,sum=1e18;
scanf("%lld%lld",&n,&x);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=1;i<=n;i++)
{
dp[i][0]=a[i];
for(int k=1;k<=n-1;k++)
{
int pos=i-k;
if(pos<=0) pos=n+pos;
dp[i][k]=min(dp[i][k-1],a[pos]);
}
}
for(int k=0;k<=n-1;k++)
{
ll ans=0;
for(int i=1;i<=n;i++)
ans+=dp[i][k];
sum=min(sum,ans+k*x);
}
printf("%lld\n",sum);
return 0;
}