无题II hdu 2236(二分枚举区间)

时间:2022-09-27 16:27:07
分析:只需要用二分找一个区间,然后不断枚举这个区间是否可以达到最大匹配,一直二分到答案为止。
 
代码:

====================================================================================

#include<stdio.h>
#include<string.h> const int MAXN = ;
const int oo = 1e9+; int G[MAXN][MAXN], Ly[MAXN];
int Max, Min, N;
bool used[MAXN]; bool Find(int i)
{
for(int j=; j<=N; j++)
{
if(!used[j] && G[i][j] >= Min && G[i][j] <= Max)
{
used[j] = true; if(!Ly[j] || Find(Ly[j]))
{
Ly[j] = i;
return true;
}
}
} return false;
}
int XYL()
{
memset(Ly, , sizeof(Ly));
int ans = ; for(int i=; i<=N; i++)
{
memset(used, false, sizeof(used));
if(Find(i) == true)
ans++;
} return ans;
} int main()
{
int T; scanf("%d", &T); while(T--)
{
int i, j, MinL=oo, MaxR=-oo; scanf("%d", &N); for(i=; i<=N; i++)
for(j=; j<=N; j++)
{
scanf("%d", &G[i][j]);
if(MinL > G[i][j])MinL = G[i][j];
if(MaxR < G[i][j])MaxR = G[i][j];
} int L=, R=MaxR-MinL, ans=; while(L <= R)
{
int Mid = (R+L)>>; for(i=; i<=MaxR-Mid; i++)
{
Min = i, Max = i+Mid; if(XYL() == N)break;
} if(i <= MaxR-Mid)
{
ans = Mid;
R = Mid-;
}
else
L = Mid+;
} printf("%d\n", ans);
} return ;
}