2263: neighbor (graphic conclusion question)

2263: neighbor

Time Limit: 1 Sec Memory Limit: 256 MB
Commits: 205 Resolved: 56
[ Commit ][ Status ][ Discussion Board ][Assert By: admin ]

Topic description

The topographic map of the school next door can be represented by a height matrix. Each position in the matrix has a number h i , and j represents the altitude of this coordinate. We will call it an altitude map. It is easy to find that we can easily calculate the next door through this matrix Main view of the school, left view.
      On the contrary, we can't uniquely determine the elevation map through the main view and the left view. Now the problem is coming. Knowing the left view of the main view, we need to know the upper and lower cost limits of the school next door (that is, the maximum and minimum possible volumes). value)

enter

The first line has two numbers n , m (1 <=n , m<=1000, 0<= h i , j <= 1000), which represent the length and width of the elevation map, respectively.

The second line has n numbers, describing the height of each position of the main view.

The third line of m numbers describes the height of each position of the left view

output

A line with two numbers, representing the minimum and maximum cost, respectively.

sample input

2 2
1 1
1 1

Sample output

2 4
#include <iostream>
#include <cstring>

using namespace std ; 

#define maxn 1100
int a[maxn] , b[maxn] ; 
int num[maxn][maxn] ; 

bool visit[maxn] ; 
int n , m ; 


int main(){

    cin >> n >> m ; 

    for(int i=1 ; i<=n ; i++){
        cin >> a[i] ; 
    }

    for(int i=1 ; i<=m ; i++){
        cin >> b[i] ; 
    }
    /* The maximum value of the corresponding row and column position is the minimum value seen by the two views,
    * The minimum value of this row and column position is 0
    */
    for(int i = 1 ; i <= n ; i ++ ) {
        for(int j = 1 ; j <= m ; j ++) {
            num [i] [j] = min (a [i], b [j]);
        }
    }

    int min_total = 0 ; 
    for(int i = 1 ; i <= n ; i ++) {
        min_total += a[i] ; 
    }
    for(int i = 1 ; i <= m ; i ++) {
        min_total += b[i] ; 
    }

    memset(visit , false , sizeof(visit)) ; 

    for ( int i= 1 ; i<=n ; i++ ){
         for ( int j= 1 ; j<=m ; j++ ){
             /* Add the height of the front view and the left view first, and each line when the volume is the smallest Take only one small square per column
            * (each eligible small square is counted twice)
            */
            if(!visit[j]&&num[i][j] == a[i]&&b[j] == num[i][j] ){
                visit[j] = true ; 
                min_total -= a[i] ; 
                break ; 
            }
        }
    }

    cout<<min_total<<endl ; 

    int max_total = 0 ; 
    for(int i=1 ; i<= n ; i++){
        for(int j=1 ; j<=m ; j++){
            max_total += num[i][j] ; 
        }
    }

    cout<<max_total<<endl ; 

    return 0 ; 
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324652662&siteId=291194637