QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#288232 | #5446. 琪露诺的符卡交换 | catagory# | 0 | 0ms | 0kb | C++23 | 7.9kb | 2023-12-22 11:43:49 | 2024-07-04 03:14:40 |
answer
#include<bits/stdc++.h>
#define LL long long
#define SZ(x) ((LL)(x.size()))
using namespace std;
long long read(){
long long q=0,w=1;
char ch=getchar();
while(ch>'9' || ch<'0'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){q=q*10+(ch-'0');ch=getchar();}
return q*w;
}
void write(LL x){
if(x<0){putchar('-');x=(-x);}
if(x>9)write(x/10);
putchar('0'+x%10);
}
void writeln(LL x){write(x);puts("");}
void writecs(LL x){write(x);putchar(' ');}
void lsh(vector<LL>&vc){
sort(vc.begin(),vc.end(),[&](LL x,LL y){return x<y;});
vc.erase(unique(vc.begin(),vc.end()),vc.end());
return ;
}
namespace dinic{
const long long N = 100000+95 , M = 500000+95;
LL n,s,t;
struct Edge{
LL to,nxt,w,c;
}e[M<<1];LL head[N],tot=1,cur[N];
void add(LL u,LL v,LL w,LL c){
e[++tot].to=v;e[tot].nxt=head[u];
e[tot].w=w;e[tot].c=c;head[u]=tot;return ;
}
void add_e(LL u,LL v,LL w,LL c){
add(u,v,w,c);add(v,u,0,-c);
}
void clear(){
for(LL i=1;i<=n;i++)head[i]=cur[i]=0;
n=s=t=0;tot=1;return ;
}
LL dis[N];bool vis[N];
bool spfa(){
for(LL i=1;i<=n;i++){dis[i]=(LL)(1e18);cur[i]=head[i];}
queue<LL>q;q.push(s);dis[s]=0;vis[s]=1;
while(!q.empty()){
LL x=q.front();q.pop();vis[x]=0;
for(LL i=head[x];i;i=e[i].nxt)
if(dis[e[i].to]>dis[x]+e[i].c&&e[i].w){
dis[e[i].to]=dis[x]+e[i].c;
if(!vis[e[i].to]){q.push(e[i].to);vis[e[i].to]=1;}
}
}
return (dis[t]!=(LL)(1e18));
}
bool inq[N];
LL dfs(LL x,LL flow){
if(x==t)return flow;
inq[x]=1;LL lastflow=flow;
for(LL i=cur[x];i&&flow;i=e[cur[x]].nxt){
cur[x]=i;
if(dis[e[i].to]==dis[x]+e[i].c&&e[i].w&&!inq[e[i].to]){
LL d=dfs(e[i].to,min(flow,e[i].w));
if(d){e[i].w-=d;e[i^1].w+=d;flow-=d;}
}
}
inq[x]=0;
if(lastflow==flow)dis[x]=(LL)(1e18);
return lastflow-flow;
}
LL solve(){
LL maxflow=0,mincost=0;
while(spfa()){
LL d=dfs(s,(LL)(1e18));
maxflow+=d;
mincost+=d*dis[t];
}
// cerr<<" maxflow = "<<maxflow<<" mincost = "<<mincost<<endl;
return maxflow;
}
}
namespace TP{
const long long N = 200+95;
LL lim;vector<LL>E[N];LL matchL[N],matchR[N];
void clear(){
for(LL i=1;i<=lim;i++){E[i].clear();matchL[i]=matchR[i]=0;}
return ;
}
void add_e(LL x,LL y){E[x].push_back(y);return ;}
bool vis[N];
bool dfs(LL x){
if(vis[x])return 0;
vis[x]=1;
for(auto y:E[x]){
if(!matchR[y]){matchL[x]=y;matchR[y]=x;return 1;}
if(dfs(matchR[y])){matchL[x]=y;matchR[y]=x;return 1;}
}
return 0;
}
void solve(LL CNT=-1){
LL cnt=0;
for(LL t=1;t<=lim;t++){
for(LL x=1;x<=lim;x++)vis[x]=0;
cnt+=dfs(t);
}
if(CNT!=-1)assert(CNT==cnt);
return ;
}
}
#define x1 x_tmp_1
#define y1 y_tmp_1
struct pt{LL x,y;};
const long long N = 200+95;
long long T,n;vector<pt>a[N][N];bool b[N][N];vector<vector<LL>>ans;LL __c[N][N];
void make(LL x0,LL y0,LL x1,LL y1){
// cout<<" x0 = "<<x0<<" y0 = "<<y0<<" x1 = "<<x1<<" y1 = "<<y1<<endl;
if(x0==x1&&y0==y1){
assert(SZ(a[x0][y0])&&!b[x0][y0]);
a[x0][y0].pop_back();
b[x0][y0]=1;return ;
}
// cout<<" SZ(a[x0][y0]) = "<<SZ(a[x0][y0])<<endl;
// cout<<" SZ(a[x1][y1]) = "<<SZ(a[x1][y1])<<endl;
// cout<<" b[x0][y1] = "<<b[x0][y1]<<" b[x1][y0] = "<<b[x1][y0]<<endl;
assert(SZ(a[x0][y0])&&SZ(a[x1][y1])&&!b[x0][y1]&&!b[x1][y0]);
pt X=a[x0][y0].back();a[x0][y0].pop_back();
pt Y=a[x1][y1].back();a[x1][y1].pop_back();
b[x0][y1]=b[x1][y0]=1;
ans.push_back({X.x,X.y,Y.x,Y.y});
return ;
}
#define PLL pair<LL,LL>
#define mp(a,b) make_pair(a,b)
#define fir first
#define sec second
#define random(a,b) (rand()%(b-a+1)+a)
int main(){
random_device seed;srand(seed());
T=read();
while(T--){
ans.clear();
n=random(1,20);
for(LL i=1;i<=n;i++)
for(LL j=1;j<=n;j++)__c[i][j]=i;
for(LL i=1;i<=n;i++)
swap(__c[i][n],__c[random(1,n)][n]);
n=read();
for(LL i=1;i<=n;i++)
for(LL j=1;j<=n;j++)__c[i][j]=read();
TP::lim=n;
for(LL i=1;i<=n;i++)
for(LL j=1;j<=n;j++){a[i][j].clear();b[i][j]=0;}
for(LL i=1;i<=n;i++)
for(LL j=1;j<=n;j++){a[i][__c[i][j]].push_back((pt){i,j});}
/* cout<<n<<endl;
for(LL i=1;i<=n;i++){
for(LL j=1;j<=n;j++)cout<<__c[i][j]<<" ";
cout<<endl;
}*/
/* {
vector<LL>ord(n+2);vector<LL>vis(n+2);for(LL i=1;i<=n;i++)vis[i]=0;
for(LL v=1;v<=n;v++)
for(LL i=1;i<=n;i++)
if(!vis[i]&&SZ(a[i][v])>=(n-1)){ord[v]=i;vis[i]=1;break;}
vector<pt>tmp[n+2][n+2];
for(LL i=1;i<=n;i++)
for(LL j=1;j<=n;j++)tmp[i][j]=a[i][j];
for(LL i=1;i<=n;i++)
for(LL j=1;j<=n;j++)a[i][j]=tmp[ord[i]][j];
}*/
/* cout<<endl;
cout<<" a : "<<endl;
for(LL i=1;i<=n;i++){
for(LL j=1;j<=n;j++)cout<<SZ(a[i][j])<<" ";
cout<<endl;
}
cout<<endl;*/
vector<LL>idxL(n+2),idxR(n+2);LL Id=0,S=0,T=0;
vector<vector<LL>>idL(n+2),idR(n+2);
S=++Id;T=++Id;
for(LL i=1;i<=n;i++){
idxL[i]=++Id;idL[i].resize(n+2);
for(LL j=1;j<=n;j++)idL[i][j]=++Id;
}
for(LL i=1;i<=n;i++){
idxR[i]=++Id;idR[i].resize(n+2);
for(LL j=1;j<=n;j++)idR[i][j]=++Id;
}
for(LL t=1;t<=n;t++){
/* vector<vector<PLL>>ext(n+2);
for(LL i=1;i<=n;i++)ext[i].resize(n+2);
for(LL i=1;i<=n;i++)
for(LL j=1;j<=n;j++)ext[i][j]=mp(-1,-1);*/
dinic::clear();dinic::n=Id;dinic::s=S;dinic::t=T;
for(LL i=1;i<=n;i++){
if(!SZ(a[i][t]))continue;
dinic::add_e(S,idxL[i],SZ(a[i][t]),0);
for(LL k=1;k<=n;k++)
if((!SZ(a[i][k]))&&(!b[i][k]))dinic::add_e(idxL[i],idL[i][k],1,0);
}
for(LL j=1;j<=n;j++){
if(b[j][t])continue;
dinic::add_e(idxR[j],T,1,0);
for(LL k=1;k<=n;k++){
if(SZ(a[j][k])>1)dinic::add_e(idR[j][k],idxR[j],((SZ(a[j][k])-1)),k);
if(SZ(a[j][k])>=1)dinic::add_e(idR[j][k],idxR[j],1,k);
}
}
vector<vector<LL>>vec;
for(LL i=1;i<=n;i++)
if(SZ(a[i][t]))
for(LL j=1;j<=n;j++)
for(LL k=t+1;k<=n;k++){
vec.push_back({dinic::tot+1,i,j,k});
dinic::add_e(idL[i][k],idR[j][k],1,0);
}
for(LL i=1;i<=n;i++){
vec.push_back({dinic::tot+1,i,i,t});
dinic::add_e(idxL[i],idxR[i],1,0);
}
LL cnt=0;
for(LL i=1;i<=n;i++)
if(!b[i][t])cnt++;
LL maxflow=dinic::solve();assert(cnt==maxflow);
for(LL i=0;i<SZ(vec);i++)
if(!dinic::e[vec[i][0]].w)
make(vec[i][1],t,vec[i][2],vec[i][3]);
/* for(LL i=1;i<=n;i++)
if(!b[i][t]){
assert(SZ(a[i][t]));
make(i,t,i,t);
}*/
/* cout<<"------------------------------------> t = "<<t<<" : "<<endl;
for(LL i=1;i<=n;i++){
for(LL j=1;j<=n;j++)cout<<SZ(a[i][j])<<" ";
cout<<endl;
}
cout<<" ----------- "<<endl;
for(LL i=1;i<=n;i++){
for(LL j=1;j<=n;j++)cout<<b[i][j]<<" ";
cout<<endl;
}
cout<<endl;*/
}
writeln(SZ(ans));
for(LL t=0;t<SZ(ans);t++){
for(LL i=0;i<SZ(ans[t]);i++)writecs(ans[t][i]);
puts("");
}
for(LL t=0;t<SZ(ans);t++){
LL x0=ans[t][0],y0=ans[t][1],x1=ans[t][2],y1=ans[t][3];
swap(__c[x0][y0],__c[x1][y1]);
}
for(LL t=1;t<=n;t++){
vector<LL>vec(n);
for(LL i=1;i<=n;i++)vec[i-1]=__c[t][i];
lsh(vec);assert(SZ(vec)==n);
}
}
return 0;
}
/*
my test data:
1
4
1 1 1 2
2 2 2 4
3 3 3 1
4 4 4 3
1
8
1 1 1 1 1 1 1 6
2 2 2 2 2 2 2 5
3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 8
5 5 5 5 5 5 5 1
6 6 6 6 6 6 6 7
7 7 7 7 7 7 7 4
8 8 8 8 8 8 8 2
1
10
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 8
3 3 3 3 3 3 3 3 3 4
4 4 4 4 4 4 4 4 4 6
5 5 5 5 5 5 5 5 5 9
6 6 6 6 6 6 6 6 6 2
7 7 7 7 7 7 7 7 7 3
8 8 8 8 8 8 8 8 8 10
9 9 9 9 9 9 9 9 9 7
10 10 10 10 10 10 10 10 10 5
*/
详细
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
7 132 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 96 ...
output:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%