2363: 瀹岀編鏃楁墜闃熷垪

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2363: 瀹岀編鏃楁墜闃熷垪相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/limit' title='limit'>limit   can   center   IV   閫夋嫨   bbs   csharp   for   include   

2363: 瀹岀編鏃楁墜闃熷垪

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 145  Solved: 7
[Submit][Status][Web Board]

Description

YT澶у瀹氫簬5鏈?6鏃ヤ妇琛屾牎杩愬姩浼氥€傚鏍℃湁 n 涓郴銆傜粍濮斾細瑕佹眰姣忎釜绯绘湁 m 涓繍鍔ㄥ憳鍙傚姞寮€骞曞紡锛屽苟涓旀瘡涓郴鐨?m 涓繍鍔ㄥ憳绔欐垚涓€闃熴€傛垜浠亣璁?n*m 鍚嶈繍鍔ㄥ憳绔欐垚涓€涓猲琛宮鍒楃殑闃熷垪锛岃〃绀轰负Anm锛氫笅鍥句腑鐨勬瘡涓€琛屼唬琛ㄤ竴涓郴銆?/p>

 

a11 a12 a13 鈥?/span> a1m

a21 a22 a23 鈥?/span> a2m

鈥?/span>  鈥?/span>  鈥?/span>  鈥?/span> 鈥?/span>

an1 an2 an3 鈥?/span> anm

 

鐜扮粍濮斾細瑕佹眰姣忕郴鍦?m 涓繍鍔ㄥ憳涓€夊嚭涓€鍚嶆棗鎵嬬珯鍦ㄦ湰绯荤殑鍓嶉潰锛屼负浜嗚瑙変笂鐨勭編瑙傦紝瑕佹眰鐩搁偦鐨勬棗鎵嬭韩楂樺樊璺濆敖鍙兘鐨勫皬锛屽舰鎴愪竴涓畬缇庢棗鎵嬮槦鍒椼€傛瘮濡傛垜浠粠涓婅堪闃熷垪涓€夋嫨鍑簕a12, a24, a33, 鈥?/span> , ank}浣滀负鏃楁墜闃熷垪銆傚垯杩檔涓汉鐨勮韩楂樺樊鏈€灏忕殑闃熷垪鏄畬缇庢棗鎵嬮槦鍒椼€傛瘮濡傛湁4涓郴锛屽悇绯婚€夋嫨鐨勬棗鎵嬪垎鍒负a锛宐锛?c锛?d锛?鍒?val=|a-b|+|b-c|+|c-d| 鏈€灏忕殑閫夋嫨涓哄畬缇庢棗鎵嬮槦鍒椼€備綘鑳藉府YT澶у閫夋嫨瀹岀編鏃楁墜闃熷垪鍚楋紵

 

Input

澶氫釜娴嬭瘯鏍蜂緥锛屾瘡涓祴璇曟牱渚嬬涓€琛屼负涓や釜鏁存暟n, m (1 <= n, m <= 1000) 锛屾帴鐫€鏄痭琛屾暣鏁版暟鍒楋紝琛ㄧず鍘熷鐨勯槦鍒楋紝鏁存暟鍊艰〃绀鸿繍鍔ㄥ憳鐨勮韩楂橈紙<=10000锛夈€?/p>

Output

瀵逛簬姣忎竴涓祴璇曟牱渚嬶紝杈撳嚭鏈€灏忕殑val鍊笺€?/p>

Sample Input

3 3
2 3 1
4 7 6
7 9 2

Sample Output

3

HINT

 

鏃楁墜鐨勮韩楂樻槸3, 4鍜?銆?/p>

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define MIN(aa,bb) aa<bb? aa:bb
#define inf 1<<30
int n,m,cap[1010][1010];
int Min[1010][1010];
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                scanf("%d",&cap[i][j]);
            sort(cap[i],cap[i]+m);
        }
        memset(Min,0,sizeof(Min));
        for(i=1;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                Min[i][j]=inf;
                int temp=lower_bound(cap[i-1],cap[i-1]+m,cap[i][j])-cap[i-1];
                if(temp<m)
                    Min[i][j]=MIN(Min[i][j],Min[i-1][temp]+abs(cap[i-1][temp]-cap[i][j]));
                if(temp>0)
                    Min[i][j]=MIN(Min[i][j],Min[i-1][temp-1]+abs(cap[i-1][temp-1]-cap[i][j]));
            }
        }
        int ans=inf;
        for(i=0;i<m;i++)
            ans=MIN(ans,Min[n-1][i]);
        printf("%d
",ans);
    }
    return 0;
}

銆€銆€



以上是关于2363: 瀹岀編鏃楁墜闃熷垪的主要内容,如果未能解决你的问题,请参考以下文章

璁板綍 瀹岀編瑙g爜 閰嶇疆

python 闃熷垪

template might not exist or might not be accessible by any of the configured Template Resolvers 瀹岀編瑙

鏁版嵁缁撴瀯涓庣畻娉曪紙Python鐗堬級锛氱敤闃熷垪锛圦ueue锛夊鐞嗙害鐟熷か闂

涓€涓畝鍗曠殑 鏀寔寤舵椂娑堟伅 ,鎸佷箙鍖栨秷鎭殑娑堟伅闃熷垪 鐨凧ava瀹炵幇

鍐掔潃鏈熸湯鎸傜鐨勯闄╀篃瑕佺粰浣犵湅鐨勬秷鎭槦鍒楀拰RocketMQ鍏ラ棬鎬荤粨