FJUT 毒瘤3(二分 + 最大匹配)题解
毒瘤3
字节跳动有n款产品,和m (m>=n)种不同的类型的客户。产品的价值由客户类型决定,第i种产品对于第j种个客户的价为值Aij.
形成一个n*m的价值矩阵。你需要为每款产品各选择一种要适应的客户。同时为最大化覆盖客户群体,且这些产品的适应客户
必须不同。问在为每个产品分配好客户类型后,把这些产品中价值第k大的数字最小能为多少。
第一行给出三个整数N,M,K(1<=K<=N<=M<=250,1<=Aij<=1e9)
接下来N行,每行M个数字,用来描述每个产品对m种客户的价值
20%的数据n+m<=20
40%的数据n+m<=40
100%的数据n<=250
输出价值第k大的数字的最小值
3 4 2
1 5 6 6
8 3 4 3
6 8 6 3
3
思路:显然肯定存在这个k,且肯定存在至少n - k + 1个匹配的权值比第k大的小。那么我们直接二分这个第k大的权值,然后把权值小于这个值的边建边跑最大匹配,如果至少有n - k + 1个匹配,那么显然这个值是可能的(不一定存在k-1个比他大)。根据题意第k大一定存在,那么最后二分出来的也一定存在。
代码:
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = + ;
const int INF = 0x3f3f3f3f;
int linker[maxn];
bool used[maxn];
int n, m, k, cnt;
struct Edge{
int to, next;
int u, v, w;
bool operator < (const Edge &x) const{
return w < x.w;
}
}edge[maxn * maxn], g[maxn * maxn];
int head[maxn], tot;
void addEdge(int u, int v){
g[tot].to = v;
g[tot].next = head[u];
head[u] = tot++;
}
bool dfs(int u){
for(int i = head[u]; i != -; i = g[i].next){
int v = g[i].to;
if(!used[v]){
used[v] = true;
if(linker[v] == - || dfs(linker[v])){
linker[v] = u;
return true;
}
}
}
return false;
}
int hungry(){
int res = ;
memset(linker, -, sizeof(linker));
for(int u = ; u <= n; u++){
memset(used, false, sizeof(used));
if(dfs(u)) res++;
}
return res;
}
bool can(int mid){
memset(head, -, sizeof(head));
tot = ;
for(int i = ; i <= cnt; i++){
if(edge[i].w <= mid){
addEdge(edge[i].u, edge[i].v);
}
else break;
}
return (hungry() >= n - k + );
}
int main(){
scanf("%d%d%d", &n, &m, &k);
cnt = ;
for(int i = ; i <= n; i++){
for(int j = ; j <= m; j++){
scanf("%d", &edge[++cnt].w);
edge[cnt].u = i, edge[cnt].v = j;
}
}
sort(edge + , edge + cnt + );
int l = , r = 1e9, ans;
while(l <= r){
int mid = (l + r) >> ;
if(can(mid)){
ans = mid;
r = mid - ;
}
else{
l = mid + ;
}
}
printf("%d\n", ans);
return ;
}
FJUT 毒瘤3(二分 + 最大匹配)题解的更多相关文章
- hdu2063 匈牙利算法 二分最大匹配模版题
过山车 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java class na ...
- POJ 3041 Asteroids(模板——二分最大匹配(BFS增广))
题目链接: http://poj.org/problem?id=3041 Description Bessie wants to navigate her spaceship through a da ...
- hdu 2444 交叉染色判断二分图+二分最大匹配
/*1A 31ms*/ #include<stdio.h> #include<string.h> #define N 300 int n; struct node { int ...
- [codeforces 852 D] Exploration Plan 解题报告 (二分+最大匹配)
题目链接:http://codeforces.com/problemset/problem/852/D 题目大意: 有V个点,N个队伍,E条边,经过每条边有个时间,告诉你初始N个队伍的位置,求至少有K ...
- loj 1150(spfa预处理+二分+最大匹配)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26864 思路:首先是spfa预处理出每个'G'到'H'的最短距离, ...
- poj1274 The Perfect Stall (二分最大匹配)
Description Farmer John completed his new barn just last week, complete with all the latest milking ...
- Treasure Exploration(二分最大匹配+floyd)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 7455 Accepted: 3 ...
- POJ 1743 Musical Theme(后缀数组 + 二分)题解
题意:一行数字,定义如下情况为好串: 1.连续一串数字,长度大于等于5 2.这行数字中多次出现这串数字的相似串,相似串为该串所有数字同加同减一个数字,如 1 2 3 和 5 6 7 3.至少有一个相似 ...
- POJ 2785 4 Values whose Sum is 0 (二分)题解
思路: 如果用朴素的方法算O(n^4)超时,这里用折半二分.把数组分成两块,分别计算前后两个的和,然后枚举第一个再二分查找第二个中是否有满足和为0的数. 注意和有重复 #include<iost ...
随机推荐
- Mysql 修改字段长度、修改列名、新增列、修改自增主键起始值
alter table 表名 modify column 字段名 类型; 例如 数据库中user表 name字段是varchar(30) 可以用 ) ; --修改字段长度 )--修改表列名 ); -- ...
- PKCS#1
ASN.1 syntax,octet string是一个8 bytes sequence string. RSA中涉及到的Data conversion: 1)I2OSP,Integer to Oct ...
- 如何登录Sitecore CMS
这是关于学习如何使用和开发Sitecore CMS的系列文章中的第一篇. 在使用Sitecore CMS之前,必须先登录.新Sitecore开发人员常见的一个问题是“我该在哪里登录?” 安装任何版本的 ...
- Java多线程-----volatile关键字详解
volatile原理 Java语言提供了一种稍弱的同步机制,即volatile变量,用来确保将变量的更新操作通知到其他线程.当把变量声明为volatile类型后, 编译器与运行时都会注意 ...
- 基础 MySQL
.一.MySQL概述 1.什么是数据库 答:数据的仓库,如:在ATM的示例中我们创建了一个 DB 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL ...
- Poj3253 Fence Repair (优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 67319 Accepted: 22142 De ...
- GUI界面修饰
function varargout = GUI20(varargin) % GUI20 MATLAB code for GUI20.fig % GUI20, by itself, creates a ...
- Django框架----模板语法
Django模板系统 官方文档 一.什么是模板? 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法分类 只需要记两种特殊符号: {{ }}和 {% %} 变量 ...
- The Little Prince-11/29
The Little Prince-11/29 The wheat fields have nothing to say to me. And that is sad. But you have ha ...
- linux下nginx编译安装
步骤: 1.获取nginx安装包. 进入nginx官网:http://nginx.org/ 找到稳定版本: 点击红框内的链接. 使用wget获取安装包. wget http://nginx.org/d ...