E. Maximize Mex
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are nn students and mm clubs in a college. The clubs are numbered from 11 to mm. Each student has a potential pipi and is a member of the club with index cici. Initially, each student is a member of exactly one club. A technical fest starts in the college, and it will run for the next dd 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 dd days. Thus, every day the director chooses such team, that the team strength is maximized.
The mex of the multiset SS is the smallest non-negative integer that is not present in SS. For example, the mex of the {0,1,1,2,4,5,9}{0,1,1,2,4,5,9} is 33, the mex of {1,2,3}{1,2,3} is 00 and the mex of ∅∅ (empty set) is 00.
Input
The first line contains two integers nn and mm (1≤m≤n≤50001≤m≤n≤5000), the number of students and the number of clubs in college.
The second line contains nn integers p1,p2,…,pnp1,p2,…,pn (0≤pi<50000≤pi<5000), where pipi is the potential of the ii-th student.
The third line contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤m1≤ci≤m), which means that ii-th student is initially a member of the club with index cici.
The fourth line contains an integer dd (1≤d≤n1≤d≤n), number of days for which the director wants to know the maximum possible strength of the team.
Each of the next dd lines contains an integer kiki (1≤ki≤n1≤ki≤n), which means that kiki-th student lefts their club on the ii-th day. It is guaranteed, that the kiki-th student has not left their club earlier.
Output
For each of the dd days, print the maximum possible strength of the team on that day.
总结:用在线的方法一直超时,各种优化都过不了,后来看了别人的做法才想到用离线,时间复杂度降到n*n,自己瞎搞了半天。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#define mem(a,x) memset(a,x,sizeof(a))
#define s1(x) scanf("%d",&x)
#define s2(x,y) scanf("%d%d",&x,&y)
#define s3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define s4(x,y,z,k) scanf("%d%d%d%d",&x,&y,&z,&k)
#define ls 2*rt
#define rs 2*rt+1
#define lson ls,L,mid
#define rson rs,mid+1,R
#define ll long long
using namespace std;
typedef pair<int,int> pii;
//inline ll ask(int x){ll res=0;while(x)res+=c[x],x-=x&(-x);return res;}
//inline void add(int x,int d){while(x<=n)c[x]+=d,x+=x&(-x);}
//int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b);}
const ll inf = 0x3f3f3f3f;
const int mx = 5e3+10;
int n,m;
int p[mx],c[mx],ans[mx],out[mx],d[mx];
bool vis[mx],has[mx];
vector<int> ve[mx];
bool dfs(int u){
for(int i = 0; i < ve[u].size(); i++){
int v = ve[u][i];
if(vis[v]) continue;
vis[v] = 1;
if(out[v] == -1 || dfs(out[v])){
out[v] = u;
return true;
}
}
return false;
}
int main(){
// freopen("C:\\Users\\black\\Desktop\\in.txt","r",stdin);
//int T=10; scanf("%d",&T);
scanf("%d%d",&n,&m);
for(int i = 1 ;i <= n; i++) s1(p[i]);
for(int i = 1 ;i <= n; i++){
s1(c[i]);
// ve[p[i]].push_back(c[i]);
}
int t;
s1(t);
for(int i = 1;i <= t; i++){
s1(d[i]);
vis[d[i]] = 1;
}
for(int i = 1 ;i <= n; i++){
if(vis[i]) continue;
//s1(c[i]);
ve[p[i]].push_back(c[i]);
}
mem(out,-1); ans[t+1] = 0;
int id;
for(int k = t; k >=1; k--){
for(int i = ans[k+1]; i <= 5000 ;i++){
mem(vis,0); //注意这个的位置
if(dfs(i) == false ){
ans[k] = i;
//printf("%d\n",i); //cout<<"out"<<endl;
break;
}
// for(int j =1; j <= 3; j++) cout<<"j="<<j<<"out="<<out[j]<<" "; puts("");
}
id = d[k];
ve[p[id]].push_back(c[id]);
}
for(int i = 1; i <= t; i++) printf("%d\n",ans[i]);
return 0;
}