Help!!! ytuoj-Perfect Bearer queue

Source: Internet
Author: User

Can't figure out why the error, oneself with written calculation a few sets of data, and then through the program test or correct (although the test data is not too large), so ask the great God to guide the passing.

Title Description

YT University is scheduled to hold a school sports meeting on May 16. There are n departments in the school. The organizing committee requires m athletes from each department to attend the opening ceremony, and the M athletes of each department stand in the first team. We assume that the n*m athlete stands in a queue of n rows m columns, represented as Anm : Each row in represents a department.

A11 A12 A13 ... .. a1m

A21 A22 A23 ... .. a2m

... .. ... .. ... .. ... .. ... ..

an1 an2 an3 ... .. ANM

The organizing committee now requests that each department m athletes elected a flag-bearer standing in front of the department, in order to visually beautiful , requires the adjacent flag-bearer height gap as small as possible to form a perfect bearer queue. For example, we choose from the above queue {A12, A24, A33, ... , Ank} as a flag-bearer queue. The smallest queue for this n person's height difference is the perfect bearer queue. For example, there are 4 departments, each of the selected standard-bearers are a, B, C, D, then val=|a-b|+|b-c|+|c-d| the smallest selection is the perfect bearer queue. Can you help YT University choose the perfect bearer queue?

Input

multiple test samples, each test sample first behavior two integers N, m (1 <= N, M <=) , followed by N An integer sequence of rows representing the original queue, the integer value indicating the height of the athlete ( <=10000 ).

Output

for each test sample, the output of the smallest Val values.

Sample input
3 32 3 14 7 67 9 2
Sample output
3
Tips

the height of the flag bearer is 3, 4 and the 2 .

My Code:

#include <iostream> #include <cmath> #define MAXN 1000using namespace Std;int a[maxn][maxn];int VAL[MAXN]; int main () {    int n,m;    int min=100000000;    cin>>n>>m;    for (int i=0;i<n;i++)        val[i]=0;    for (int i=0, i<n; i++) for        (int j=0; j<m; j + +)            cin>>a[i][j];    for (int i=0, i<n; i++)    {for        (int j=0; j<m-1; j + +)        {            val[i]+= (fabs (a[i][j]-a[i][j+1]));        }    }    for (int i=0; i<n; i++)    {        if (val[i]<min)            min=val[i];    }    cout<<min;    return 0;}


Error message:

Help!!! ytuoj-Perfect Bearer queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.