河南省第十一届ACM竞赛 C山区修路

传送门
 

题面:

 

画一个表格,一目了然

代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <iomanip>
#include <bitset>
using namespace std;

#define eps         (1e-6)
#define LL          long long
#define pi          acos(-1.0)
#define ALL(a)      (a.begin(),(a.end())
#define ZERO(a)     memset(a,0,sizeof(a))
#define MINUS(a)    memset(a,0xff,sizeof(a))
#define IOS         cin.tie(0),cout.sync_with_stdio(0)
#define PRINT(a,b)  cout << "#" << (a) << " " << (b) << endl
//#define DEBUG(a,b)  cout << "$" << (a) << " " << (b) << endl
#define lin       cout << "\n--------------------\n"

const LL LLINF = 1e18+100;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 5;
const int MOD = 1000000000+7;
#define esp 1e-6

int H[MAXN] = {0};
int dp[MAXN][100];

int main()
{
    int t;
    int n,W;
    cin >> t;
    while(t--){
        cin >> n >> W;
        ZERO(H);
        ZERO(dp);
        for(int i=1; i<=n; ++i){
            scanf("%d", &H[i]);
        }
        H[0] = H[1];
        
        for(int i=H[1]; i<=100; ++i){
            dp[1][i] = (i-H[1])*(i-H[1]);
        }
        
        for(int i=2; i<=n; ++i){
            for(int j=1; j<=100; ++j){
                if(j<H[i]) continue;
                
                for(int k=1; k<=100; ++k){
                    if(k<H[i-1]) continue;
                    if(k==H[i-1]){
                        dp[i][j] = dp[i-1][k] + abs(j-k)*W; //
                    }
                    else{
                        dp[i][j] = min(dp[i][j], dp[i-1][k] + abs(j-k)*W); //
                    }
                }
                //cout << "#" << i << " " << j << ": " << dp[i][j] << endl;
                dp[i][j] += (j-H[i])*(j-H[i]); //垫高的花费
                //cout << "#" << i << " " << j << ": " << dp[i][j] << endl;
            }
            
        }
        
        int ans = INF;
        for(int i=H[n]; i<=100; ++i){
            ans = min(dp[n][i], ans);
            //cout << dp[n][i] << endl;
        }
        cout << ans << endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值