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 ...
随机推荐
- 恢复Windows10应用商店
用管理员权限运行powershell,输入 Get-AppxPackage -AllUsers| Foreach {Add-AppxPackage -DisableDevelopmentMode -R ...
- Centos 6.5初始化配置
安装好centos 6.5 # -*- coding:utf-8 -*- import win32api import time import os from Tkinter import * are ...
- Permutation Bo (数学证明)
当在两端时:共有n * (n - 1)种组合,满足条件的有,计算可得, counter = n * (n - 1) / 2. 其他位置时:共有n * (n - 1) * (n - 2) 种组合,满足条 ...
- KindEditor echarts
var editor; KindEditor.ready(function (K) { editor = K.create('textarea[name="content"]', ...
- 用django统计代码行数+注释行数
实现统计代码行数: 1.首先在url.py中配置 from django.conf.urls import url from django.contrib import admin from app0 ...
- 什么是Unicode
写这篇博客的原因, 从做软件开始,什么ASCII码, Unicode,UTF-8,UTF-16,UTF-32......这些鬼东西总是经常碰到,只知道这些鬼是编码格式,其他的就啥都不清楚了,既然总是遇 ...
- tiny4412 硬件解码
今天发现了一个好的资源,上面有三星的一些实例代码.http://git.infradead.org/users/kmpark/public-apps
- 解读 JavaScript 之引擎、运行时和堆栈调用
https://www.oschina.net/translate/how-does-javascript-actually-work-part-1 随着 JavaScript 变得越来越流行,很多团 ...
- winscp 怎么用私钥文件登录的,以.ppk结尾的密钥文件
Winscp默认用帐号密码登录,用私钥文件登录需要在高级选项里面的SSH--验证里面选择文件. Winscp使用的是putty作为SSH登录工具,而puttygen所生成的是以.ppk结尾的密钥文件. ...
- AtCoder Beginner Contest 087 (ABC)
A - Buying Sweets 题目链接:https://abc087.contest.atcoder.jp/tasks/abc087_a Time limit : 2sec / Memory l ...