[SCOI2015] Little Convex Play Matrix (Hungary + Two Points)

description

Title description

Xiao Convex and Xiao Fang are good friends. Xiao Fang gives Xiao Convex an N×M (N≤M) matrix A, and asks Xiao Convex to select N numbers from it. Any two numbers cannot be in the same row or column. , Now Xiao convex wants to know the minimum value of the K-th largest number among the selected N numbers.

Input format The
first line gives three integers N, M, K. The next N rows, each with M numbers, are used to describe this matrix.
Output format
Output the minimum value of the K-th largest number among the selected N numbers.

Sample
Input
3 4 2
1 5 6 6
8 3 4 3
6 8 6 3
Output
3

Data range and prompt
1≤K≤N≤M≤250,1≤Ai,j≤10^9

solution

The first KKThe minimum value of K is large, and the obvious routine is to think about
dichotomy. Might as well dichotomy the final answer.
Then the question is transformed into the maximum number of matches when the edge weight of the selected edge does not exceed the answer≥ n − k + 1 \ge n -k+1nk+1

code

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define maxn 255
int n, m, k, l, r;
bool vis[maxn];
int match[maxn];
int matrix[maxn][maxn];

bool find( int x, int lim ) {
    
    
	for( int i = 1;i <= m;i ++ ) {
    
    
		if( ! vis[i] && matrix[x][i] <= lim ) {
    
    
			vis[i] = 1;
			if( ! match[i] || find( match[i], lim ) ) {
    
    
				match[i] = x;
				return 1;
			}
		}
	}
	return 0;
}

int check( int x ) {
    
    
	memset( match, 0, sizeof( match ) );
	int ans = 0;
	for( int i = 1;i <= n;i ++ ) {
    
    
		memset( vis, 0, sizeof( vis ) );
		if( find( i, x ) ) ans ++;
	}
	return ans;
}

int main() {
    
    
	scanf( "%d %d %d", &n, &m, &k );
	for( int i = 1;i <= n;i ++ )
		for( int j = 1;j <= m;j ++ ) {
    
    
			scanf( "%d", &matrix[i][j] );
			r = max( r, matrix[i][j] );
		}
	int ans;
	while( l <= r ) {
    
    
		int mid = ( l + r ) >> 1;
		if( check( mid ) >= n - k + 1 ) ans = mid, r = mid - 1;
		else l = mid + 1;
	}
	printf( "%d\n", ans );
	return 0;
}

Guess you like

Origin blog.csdn.net/Emm_Titan/article/details/113687840