Amazon amazon online test questions

Original link: http://www.cnblogs.com/diegodu/p/4657991.html

 

 

 

 

Analysis: in fact, find the minimum weighted distance is a rectangle in a point to another point

 

method one:

For each point of seeking the weighted distance to another point, and then compare the minimum. Because of M * N points A weighted distance of each point is O (M * N), so that the overall time complexity is O (M * M * N * N) a.

Method Two:

First preprocessing, calculate how many points each row, each column is the number of points, then calculate the weighted point other lines moved from my line need to point the other columns moved away from my weight this column ,

Then for each point, calculates the cost of its row is moved to the column of the cost and +, the minimum is obtained. Time complexity O (M * N).

 

 

code see below, calcMIN is a method, calcDiego method two.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define MAX_SIZE 500

int arr[MAX_SIZE][MAX_SIZE];
int sumOfRow[MAX_SIZE];
int sumOfCol[MAX_SIZE];
int costOfRow[MAX_SIZE];
int costOfCol[MAX_SIZE];

int rtnRow;
int rtnCol;

int calcCost(int row, int col, int x, int y)
{
    int cost = 0;
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            if(arr[i][j] != 0)
            {
                cost += (abs(x-i) + abs(y-j)) * arr[i][j];
            }
        }
    }
    return cost;
}

void calcMin(int row, int col)
{
    long long minCost = LLONG_MAX;
    long long tmpCost = 0;
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            tmpCost = calcCost(row, col, i, j);
            if(tmpCost < minCost)
            {
                minCost = tmpCost;
                rtnRow = i;
                rtnCol = j;
            }
        }
    }
}



void calcDiego(int row, int col)
{
    for(int i = 0; i < row; i ++)
    {
        sumOfRow[i] = 0;
        costOfRow[MAX_SIZE] = 0;
    }

    for(int j = 0; j < col; j++)
    {
        sumOfCol[j] = 0;
        costOfCol[MAX_SIZE] = 0;
    }

    for(int i = 0; i < row; i ++)
    {
        for(int j = 0; j < col; j++)
        {
            sumOfRow[i] += arr[i][j];
            sumOfCol[j] += arr[i][j];
        }
    }


    for(int i = 0; i < row; i ++)//cost for moving row j to row i
    {
        for(int j = 0; j < row; j ++)
        {
            if(i == j || sumOfRow[j] == 0)
                continue;
            costOfRow[i] += (abs(j-i)) * sumOfRow[j];
        }
    }

    for(int i = 0; i < col; i ++)//cost for moving col j to col i
    {
        for(int j = 0; j < col; j ++)
        {
            if(i == j || sumOfCol[j] == 0)
                continue;
            costOfCol[i] += (abs(j-i)) * sumOfCol[j];
        }
    }

    int cost = INT_MAX;
    for(int i = 0; i < row; i ++)
    {
        for(int j = 0; j < col; j ++)
        {
            if(costOfRow[i] + costOfCol[j] < cost)
            {
                cost = (costOfRow[i] + costOfCol[j] );
                rtnRow = i;
                rtnCol = j;
            }
        }
    }

}


int main ()
{
    int i, j;
    int row, col;
    cout << "INT_MAX\t" << INT_MAX << endl;
    cout << (500ULL*500*500000) << endl;
    while (scanf("%d%d", &row, &col) == 2) {
        for (i = 0; i < row; i++)
        {
            for (j = 0; j < col; j++)
            {
                scanf("%d", &arr[i][j]);
            }
        }
        //printf("calc\n " );
        calcMin(row, col);
        printf("%d %d\n", rtnRow, rtnCol);
        calcDiego(row, col);
        printf("%d %d\n", rtnRow, rtnCol);

    }

    return 0;
}

 

Reproduced in: https: //www.cnblogs.com/diegodu/p/4657991.html

Guess you like

Origin blog.csdn.net/weixin_30432179/article/details/94792212