我发现我有道叫[SCOI2010]连续攻击游戏的题白写了..

Description

There are \(n\) students and \(m\) clubs in a college. The clubs are numbered from \(1\) to \(m\). Each student has a potential \(p_i\) and is a member of the club with index \(c_i\). Initially, each student is a member of exactly one club. A technical fest starts in the college, and it will run for the next \(d\) days. There is a coding competition every day in the technical fest.

Every day, in the morning, exactly one student of the college leaves their club. Once a student leaves their club, they will never join any club again. Every day, in the afternoon, the director of the college will select one student from each club (in case some club has no members, nobody is selected from that club) to form a team for this day's coding competition. The strength of a team is the mex of potentials of the students in the team. The director wants to know the maximum possible strength of the team for each of the coming dddays. Thus, every day the director chooses such team, that the team strength is maximized.

The mex of the multiset \(S\) is the smallest non-negative integer that is not present in \(S\). For example, the mex of the \(\{0,1,1,2,4,5,9\}\) is \(3\), the mex of \(\{1,2,3\}\) is \(0\) and the mex of \(\varnothing\) (empty set) is \(0\).

Input

The first line contains two integers \(n\) and \(m\) (\(1\le m\le n\le 5000\)), the number of students and the number of clubs in college.

The second line contains \(n\) integers \(p_1,p_2,…,p_n\) (\(0\le p_i<5000\)), where \(p_i\) is the potential of the \(i\)-th student.

The third line contains \(n\) integers \(c_1,c_2,…,c_n\) (\(1\le c_i\le m\)), which means that \(i\)-th student is initially a member of the club with index \(c_i\).

The fourth line contains an integer \(d\) (\(1\le d\le n\)), number of days for which the director wants to know the maximum possible strength of the team.

Each of the next \(d\) lines contains an integer \(k_i\) (\(1\le k_i\le n\)), which means that \(k_i\)-th student lefts their club on the \(i\)-th day. It is guaranteed, that the \(k_i\)-th student has not left their club earlier.

Output

For each of the \(d\) days, print the maximum possible strength of the team on that day.

Examples

input

5 3
0 1 2 2 0
1 2 2 3 2
5
3
2
4
5
1

output

3
1
1
1
0

input

5 3
0 1 2 2 1
1 3 2 3 2
5
4
2
3
5
1

output

3
2
2
1
0

input

5 5
0 1 2 4 5
1 2 3 4 5
4
2
3
5
4

output

1
1
1
1

Note

Consider the first example:

On the first day, student \(3\) leaves their club. Now, the remaining students are \(1\), \(2\), \(4\) and \(5\). We can select students \(1\), \(2\) and \(4\) to get maximum possible strength, which is \(3\). Note, that we can't select students \(1\), \(2\) and \(5\), as students \(2\) and \(5\) belong to the same club. Also, we can't select students \(1\), \(3\) and \(4\), since student \(3\) has left their club.

On the second day, student \(2\) leaves their club. Now, the remaining students are \(1\), \(4\) and \(5\). We can select students \(1\), \(4\) and \(5\) to get maximum possible strength, which is \(1\).

On the third day, the remaining students are \(1\) and \(5\). We can select students \(1\) and \(5\) to get maximum possible strength, which is \(1\).

On the fourth day, the remaining student is \(1\). We can select student \(1\) to get maximum possible strength, which is \(1\).

On the fifth day, no club has students and so the maximum possible strength is \(0\).

题意

有 \(n\) 个学生,\(m\) 个社团。每个学生属于一个社团。在接下来的 \(d\) 天里,每天会有一个人退团。每天从每个社团中最多选出一个人,使得选出的人的能力值集合 \(\{p_i\}\) 的 \(\mathrm{mex}\) 值最大。求出每天的最大 \(\mathrm{mex}\) 值。

题解

可能建立二分图模型是求 \(\mathrm{mex}\) 的一个套路/技巧吧。反正是忘了…但是做题的时候也不知道哪些是经典,干脆以后全部好好落实。

因为每个能力值至少被一个社团提供,而一个社团最多提供一个能力值,由此构造二分图。左侧是能力值,右侧是社团。当存在一个学生能力值为 \(p_i\),社团为 \(c_i\) 时,从 \(p_i\) 向 \(c_i\) 连边。

但是学生是在动态变化的,而且只会减少人,由此每次询问的答案一定非严格递减。

但是二分图最大匹配的匈牙利算法是不支持删边的,而一次匹配(指左边的一个点)的复杂度就是 \(O(m)\),每次进行复杂度高达 \(O(nmd)​\),显然不是我们想要的。

考虑反过来,从最终状态加学生,每次的答案一定不会减少。那么我们就假设加完前 \(i\) 个学生后,此时的 \(\mathrm{mex}\) 是 \(t\)。在二分图左侧 Find(t),此时如果替换,只会替换比 \(t\) 小的点,不会造成更劣答案;最终会找到一个没有匹配过的社团,令答案 \(+1\)。

重新加边不会影响原来算好的答案。最终反序输出即可。

时间复杂度 \(O(nm+qm)\)。

Code:

写匈牙利一定要在每次 Find() 函数外调用 Find() 时清空 used[] 啊啊啊啊啊啊否则会 FST

#include<cstdio>
#include<cstring>
struct edge
{
int n,nxt;
edge(int n,int nxt)
{
this->n=n;
this->nxt=nxt;
}
edge(){}
}e[5050];
int head[5050],ecnt=-1;
void add(int from,int to)
{
e[++ecnt]=edge(to,head[from]);
head[from]=ecnt;
}
int s[5050];
bool used[5050];
bool Find(int x)
{
if(used[x])
return false;
used[x]=1;
for(int i=head[x];~i;i=e[i].nxt)
if(s[e[i].n]==-1||Find(s[e[i].n]))
{
s[e[i].n]=x;
return true;
}
return false;
}
int a[5050],b[5050],c[5050];
int ans[5050];
int main()
{
memset(head,-1,sizeof(head));
memset(s,-1,sizeof(s));
int n,m,q;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
for(int i=1;i<=n;++i)
scanf("%d",&b[i]);
scanf("%d",&q);
for(int i=1;i<=q;++i)
{
scanf("%d",&c[i]);
used[c[i]]=1;
}
for(int i=1;i<=n;++i)
if(!used[i])
add(a[i],b[i]);
int t=0;
for(int i=q;i>=1;--i)
{
memset(used,0,sizeof(used));
while(Find(t))
{
++t;
memset(used,0,sizeof(used));
}
ans[i]=t;
add(a[c[i]],b[c[i]]);
}
for(int i=1;i<=q;++i)
printf("%d\n",ans[i]);
return 0;
}

CF1139E Maximize Mex 题解【二分图】的更多相关文章

  1. CF1139E Maximize Mex(二分图匹配,匈牙利算法)

    好题.不过之前做过的[SCOI2010]连续攻击游戏跟这题一个套路,我怎么没想到…… 题目链接:CF原网 洛谷 题目大意:在一个学校有 $n$ 个学生和 $m$ 个社团,每个学生有一个非负整数能力值 ...

  2. CF1139E Maximize Mex

    题目地址:CF1139E Maximize Mex 这其实是一个二分图匹配匈牙利算法的巧妙运用 考虑倒序回答 则由原来的删除改为添加 把 potential 值作为左部,则一共有编号为 \(0~m\) ...

  3. Codeforces 1139E Maximize Mex 二分图匹配

    Maximize Mex 离线之后把删数变成加数, 然后一边跑匈牙利一遍算答案. #include<bits/stdc++.h> #define LL long long #define ...

  4. [CF1139 E] Maximize Mex 解题报告 (二分图匹配)

    interlinkage: https://codeforces.com/contest/1139/problem/E description: 有$n$个学生,$m$个社团,每个学生有一个能力值,属 ...

  5. codeforces1139E Maximize Mex 二分图匹配

    题目传送门 题意:给出n个人,m个社团,每个人都有一个标号,一个能力值,并且属于一个社团,第i天的凌晨,第$k_i$个人会离开.每天每个社团最多派一个人出来参加活动.派出的人的能力值集合为S,求每天$ ...

  6. codeforces#1139E. Maximize Mex(逆处理,二分匹配)

    题目链接: http://codeforces.com/contest/1139/problem/E 题意: 开始有$n$个同学和$m$,每个同学有一个天赋$p_{i}$和一个俱乐部$c_{i}$,然 ...

  7. BZOJ3339:Rmq Problem & BZOJ3585 & 洛谷4137:mex——题解

    前者:https://www.lydsy.com/JudgeOnline/problem.php?id=3339 后者: https://www.lydsy.com/JudgeOnline/probl ...

  8. 洛谷4137 mex题解 主席树

    题目链接 虽然可以用离线算法水过去,但如果强制在线不就gg了. 所以要用在线算法. 首先,所有大于n的数其实可以忽略,因为mex的值不可能大于n 我们来设想一下,假设已经求出了从0到n中所有数在原序列 ...

  9. 洛谷 P4137 Rmq Problem/mex 题解

    题面 首先,由于本人太菜,不会莫队,所以先采用主席树的做法: 离散化是必须环节,否则动态开点线段数都救不了你: 我们对于每个元素i,插入到1~(i-1)的主席树中,第i颗线段树(权值线段树)对于一个区 ...

随机推荐

  1. Basic4android v3.00 发布

    这次发布的版本主要是增加了快速debuger. 在运行时,可以在IDE 里面随时修改代码,而不需要重新发布应用. 大大提高了开发效率. Basic4android v3.00 is released. ...

  2. 懒人的ERP开发框架--2B&苦B程序员专用

    在企业内部的ERP系统开发中,如果使用MS的技术,那么Winform + DevExpress + IIS + WCF +EF 就是懒人的黄金组合了,EF使用数据库优先,一般ERP应用主要关注点在数据 ...

  3. Java中的一些代理技术

    使用cglib,asm 对接口进行拦截,这里需要调用Invoke方法 final IUserService userService=new UserService(); Enhancer enhanc ...

  4. mfc的一点总结-----Edit Control操作

    获取Edit Control(编辑框)的内容: CString key; GetDlgItem(IDC_EDIT1)->GetWindowText(key); 其中IDC_EDIT1是所要获取编 ...

  5. java.util.concurrent.locks.LockSupport (讲得比较细)

    转自:   https://my.oschina.net/readjava/blog/282882   摘要: 要学习JAVA中是如何实现线程间的锁,就得从LockSupport这个类先说起,因为这个 ...

  6. Altera FPGA 开启引脚片上上拉电阻功能

    本博文以矩阵键盘实验为例,介绍了如何开启FPGA管脚的片上上拉电阻. Cyclone IV E FPGA的通用输入输出管脚都支持内部弱上拉电阻,但是时钟输入脚不支持.所以,当需要上拉电阻的信号(如本例 ...

  7. php 图像处理库ImageMagick windows下的安装

    http://blog.sina.com.cn/s/blog_966e43000101bgqj.html

  8. Hadoop集群 -Eclipse开发环境设置

    1.Hadoop开发环境简介 1.1 Hadoop集群简介 Java版本:jdk-6u31-linux-i586.bin Linux系统:CentOS6.0 Hadoop版本:hadoop-1.0.0 ...

  9. 仿微信聊天面板制作 javascript

    先上图吧 , 点击头像更换说话对象,简单说下实现原理,html中创建一个ul用于存放所有说话的内容,对话内容是有javascript 动态生成, 主要难点:先布局好css,当时奥巴马发送时候,让这个l ...

  10. DS博客作业01--日期抽象数据类型

    1.思维导图及学习体会(2分) 1.1第一章绪论知识点思维导图 1.2学习体会 从暑假看视频到开学的预习,我感觉数据结构与c语言比起来更加抽象,更加难理解,那些概念也只能理解一些字面意思,对时间复杂度 ...