1 solutions

  • 0
    @ 2024-12-11 0:49:56

    C++ :

    #include<cstring>
    #include<cstdio>
    using namespace std;
    #define Size 100010
    int n,c,ans=9999999;
    int height[Size]={0},tall[Size][110]={0},high[Size][110]={0};
    int f[Size][110]={0};
    int Abs(int x);
    int Min(int x,int y);
    int main()
    {
    	scanf("%d%d",&n,&c);
    	int i,j,k,ki,maxhi=0;
    	for(i=1;i<=n;i++)
    	scanf("%d",&height[i]);
    	memset(tall,9999,sizeof(tall));
    	memset(high,9999,sizeof(high));
    	memset(f,9999,sizeof(f));
    	for(j=1;j<=100;j++)
    	{
    		if(j>=height[1])
    			f[1][j]=(j-height[1])*(j-height[1]);
    		high[1][j]=Min(high[1][j-1],f[1][j]-j*c);
    	}
    	for(j=100;j>=1;j--)
    	tall[1][j]=Min(tall[1][j+1],f[1][j]+j*c);
    	for(i=2;i<=n;i++)
    	{
    		for(j=1;j<=100;j++)
    		{
    			if(j>=height[i])
    			f[i][j]=Min(tall[i-1][j]-c*j,high[i-1][j]+j*c)+(j-height[i])*(j-height[i]);
    			high[i][j]=Min(f[i][j]-j*c,high[i][j-1]);
    		}
    		for(j=100;j>=1;j--)
    		tall[i][j]=Min(f[i][j]+j*c,tall[i][j+1]);
    	}
    	for(j=1;j<=100;j++)
    	ans=Min(ans,f[n][j]);
    	printf("%d\n",ans);
    	return 0;
    }
    int Min(int x,int y)
    {
    	if(x<y)	return x;
    	else return y;
    }
    int Abs(int x)
    {
    	if(x<0)	return -x;
    	else return x;
    }
    
    • 1

    Information

    ID
    764
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By