QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#20645#2617. Browsing the Collectionuezexh#WA 1ms4284kbC++172.8kb2022-02-17 07:40:492022-05-03 10:55:09

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-03 10:55:09]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4284kb
  • [2022-02-17 07:40:49]
  • 提交

answer

#include <cstdio>
#include <cstring>
#include <climits>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
template<typename T> void cmin(T &a,const T &b){if(b<a)a=b;}
template<typename T> void cmax(T &a,const T &b){if(a<b)a=b;}

const int N=500,M=5,S=1<<M;
int n,m,a[N][M],pre[N][S],nxt[N][S],ans[N][N],d[N][S],fu[N*S];
int id(int p,int s){return (p<<m)|s;}
int Find(int x){return ~fu[x]?fu[x]=Find(fu[x]):x;}
void bfs(int ed){
	static pii q[N*S],*Head,*Tail;
	static set<int> v[N*S];
	Head=Tail=q;
	memset(d,0x3f,sizeof d);
	for(int p=0;p<n;++p)
		for(int s=0;s<(1<<m);++s)
			v[Find(id(p,s))].insert(p);
	for(int s=0;s<(1<<m);++s)
		d[ed][s]=0,*Tail++={ed,s},v[Find(id(ed,s))].erase(ed);
	int cnt=0;
	while(Head!=Tail){
		auto &[p,s]=*Head++;
		if(!s){
			ans[p][ed]=d[p][s];
			if((++cnt)==n)
				return;
		}
		if(d[pre[p][s]][s]>d[p][s]+1)
			d[pre[p][s]][s]=d[p][s]+1,*Tail++={pre[p][s],s};
		if(d[nxt[p][s]][s]>d[p][s]+1)
			d[nxt[p][s]][s]=d[p][s]+1,*Tail++={nxt[p][s],s};
		for(int i=0;i<m;++i)
			if(!((s>>i)&1) && d[p][s|(1<<i)]>d[p][s]+1)
				d[p][s|(1<<i)]=d[p][s]+1,*Tail++={p,s|(1<<i)};
		for(int i=0;i<m;++i){
			if(!((s>>i)&1))
				continue;
			int t=s^(1<<i);
			v[t].insert(pre[p][s]);
			auto fd=[](const set<int> &v,int x){
				if(!v.size())
					return -1;
				auto it=v.upper_bound(x);
				if(it==v.begin())
					return *v.rbegin();
				return *--it;
			};
			int g=Find(id(p,t));
			v[g].insert(pre[p][s]);
			for(int q=fd(v[g],p);q!=-1 && (q==p || q!=pre[p][s]);q=fd(v[g],q?q-1:n-1)){
				if(d[q][t]>d[p][s]+1)
					d[q][t]=d[p][s]+1,*Tail++={q,t};
				v[g].erase(q);
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;++i)
		for(int j=0;j<m;++j)
			scanf("%d",a[i]+j);
	memset(fu,0xff,sizeof fu);
	for(int s=0;s<(1<<m);++s){
		map<vector<int>,int> lst;
		for(int p=0;p<n;++p){
			vector<int> v;
			for(int i=0;i<m;++i)
				if((s>>i)&1)
					v.push_back(a[p][i]);
			auto it=lst.find(v);
			if(it!=lst.end()){
				fu[id(p,s)]=id(it->second,s);
				pre[p][s]=it->second;
				it->second=p;
			}else{
				lst.insert({v,p});
			}
		}
		for(int p=0;p<n;++p){
			vector<int> v;
			for(int i=0;i<m;++i)
				if((s>>i)&1)
					v.push_back(a[p][i]);
			auto it=lst.find(v);
			if(it!=lst.end()){
				pre[p][s]=it->second;
				lst.erase(it);
			}
		}
		for(int p=0;p<n;++p)
			if(pre[p][s]>=p){
				nxt[pre[p][s]][s]=p;
				for(int q=pre[p][s];q!=p;q=pre[q][s])
					nxt[pre[q][s]][s]=q;
			}
	}
	for(int i=0;i<n;++i)
		for(int j=0;j<n;++j)
			ans[i][j]=INT_MAX;
	for(int p=0;p<n;++p)
		bfs(p);
	for(int i=0;i<n;++i)
		for(int j=0;j<n;++j)
			printf("%d%c",ans[i][j],j<n-1?32:10);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 4284kb

input:

9 3
5 3 7
5 3 4
5 3 7
5 3 2
5 3 4
5 3 7
2 3 7
5 3 7
2 3 7

output:

0 1 2 1 2 3 1 2 1
1 0 1 1 2 2 1 2 2
2 1 0 1 1 2 1 2 2
3 2 1 0 1 1 1 2 2
3 2 2 1 0 1 1 2 2
3 1 2 1 1 0 1 2 2
2 1 3 1 2 1 0 1 2
2 1 3 1 2 2 1 0 1
1 1 3 1 2 3 2 1 0

result:

wrong answer 17th numbers differ - expected: '3', found: '2'