QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#309452 | #8131. Filesystem | ucup-team266# | ML | 0ms | 0kb | C++14 | 3.0kb | 2024-01-20 17:25:15 | 2024-01-20 17:25:16 |
answer
/*
Things to notice:
1. do not calculate useless values
2. do not use similar names
Things to check:
1. submit the correct file
2. time (it is log^2 or log)
3. memory
4. prove your naive thoughts
5. long long
6. corner case like n=0,1,inf or n=m
7. check if there is a mistake in the ds or other tools you use
8. fileio in some oi-contest
9. module on time
10. the number of a same divisor in a math problem
11. multi-information and queries for dp and ds problems
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pii pair<long long,long long>
#define mp make_pair
#define pb push_back
const int mod=998244353;
const int inf=0x3f3f3f3f;
const int INF=1e18;
struct Dinic
{
int p[500005],to[20000005],fl[20000005],nxt[20000005],eid;
int S,T;
void ins(int u,int v,int cap)
{
to[eid]=v,fl[eid]=cap,nxt[eid]=p[u],p[u]=eid,eid++;
}
void add(int u,int v,int cap)
{
assert(S<=u&&u<=T);
assert(S<=v&&v<=T);
// cout<<"add "<<u<<" "<<v<<" "<<cap<<endl;
ins(u,v,cap),ins(v,u,0);
}
int dis[500005],cur[500005];
void clr()
{
for(int i=S;i<=T;i++) p[i]=dis[i]=-1;
for(int i=0;i<eid;i++) to[i]=fl[i]=nxt[i]=0;
eid=0;
}
vector <int> Tmp;
bool bfs()
{
queue <int> q;
while(q.size()) q.pop();
q.push(S);
dis[S]=0,Tmp.pb(S);
cur[S]=p[S];
while(q.size())
{
int u=q.front();
q.pop();
for(int i=p[u];i!=-1;i=nxt[i])
{
int v=to[i];
if(dis[v]==-1&&fl[i])
{
dis[v]=dis[u]+1,Tmp.pb(v);
cur[v]=p[v],q.push(v);
if(v==T) return 1;
}
}
}
return 0;
}
int dfs(int u,int a)
{
if(u==T) return a;
int flow=0;
for(int i=cur[u];i!=-1&&flow<a;i=nxt[i])
{
cur[u]=i;
int v=to[i];
if(dis[v]==dis[u]+1&&fl[i])
{
int tmp=dfs(v,min(fl[i],a-flow));
if(!tmp) dis[v]=-1;
else fl[i]-=tmp,fl[i^1]+=tmp,flow+=tmp;
}
}
return flow;
}
int dinic()
{
int res=0;
while(bfs())
{
int tmp=dfs(S,inf);
res+=tmp;
for(int i=0;i<Tmp.size();i++) dis[Tmp[i]]=-1;
Tmp.clear();
}
return res;
}
void init()
{
memset(p,-1,sizeof(p));
memset(to,0,sizeof(to));
memset(nxt,0,sizeof(nxt));
memset(fl,0,sizeof(fl));
memset(dis,-1,sizeof(dis));
eid=0;
}
} dc;
int n,K;
int req[1005],p[1005],vis[1005];
void solve()
{
cin>>n>>K;
dc.S=0,dc.T=3*n+1;
memset(vis,0,sizeof(vis));
for(int i=1;i<=K;i++) cin>>req[i],vis[req[i]]=1,dc.add(0,req[i],inf),dc.add(req[i],3*n+1,inf);
int uidx=n;
for(int i=1;i<=n;i++) cin>>p[i];
int tot=0;
for(int i=1;i<n;i++) if(vis[i]&&vis[i+1])
{
uidx++,tot++;
dc.add(i,uidx,INF),dc.add(i+1,uidx,INF),dc.add(uidx,3*n+1,1);
}
for(int i=1;i<n;i++) if(vis[p[i]]&&vis[p[i+1]])
{
uidx++,tot++;
dc.add(uidx,p[i],INF),dc.add(uidx,p[i+1],INF),dc.add(0,uidx,1);
}
int ans=dc.dinic();
dc.clr();
ans-=K*inf;
// cout<<ans<<" "<<tot<<"\n";
ans=tot-ans;
cout<<K-ans<<"\n";
}
signed main()
{
dc.init();
ios::sync_with_stdio(0);
cin.tie(0);
int _=1;
cin>>_;
while(_--) solve();
return 0;
}
詳細信息
Test #1:
score: 0
Memory Limit Exceeded
input:
2 12 8 2 5 8 3 4 10 12 1 10 5 8 6 4 7 2 3 11 12 1 9 8 4 1 3 5 7 1 4 5 8 7 6 3 2
output:
3 4