QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#376533#5116. Contestswjh213WA 0ms21988kbC++142.5kb2024-04-04 11:27:282024-04-04 11:27:28

Judging History

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

  • [2024-04-04 11:27:28]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:21988kb
  • [2024-04-04 11:27:28]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int const MAX=1e5+10;
int a[7][MAX],rk[7][MAX];
int bei[MAX][22][7];
int hou[7][MAX][7];
signed main(){
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++)cin>>a[i][j];
	}
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++){
			rk[i][a[i][j]]=j;
		}
	}
	for(int i=1;i<=m;i++){
		for(int j=1;j<=m;j++)hou[i][n][j]=a[i][n];
		for(int j=n-1;j>=1;j--){
			for(int k=1;k<=m;k++)hou[i][j][k]=a[i][j];
			for(int k=1;k<=m;k++){
				if(rk[k][hou[i][j+1][k]]<rk[k][hou[i][j][k]])hou[i][j][k]=hou[i][j+1][k];
			}
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			bei[i][0][j]=hou[1][rk[1][i]][j];
			//if(i==3)cerr<<i<<" "<<0<<" "<<j<<" "<<bei[i][0][j]<<" "<<1<<" "<<rk[j][i]<<" "<<j<<"\n";
		}
		for(int j=2;j<=m;j++){
			for(int k=1;k<=m;k++){
				if(rk[k][hou[j][rk[j][i]][k]]<rk[k][bei[i][0][k]])bei[i][0][k]=hou[j][rk[j][i]][k];
			}
		}
	}
	for(int i=1;i<=18;i++){
		for(int j=1;j<=n;j++){
			for(int l=1;l<=m;l++){
				bei[j][i][l]=bei[bei[j-1][i-1][1]][i-1][l];
			}
			for(int k=2;k<=m;k++){
				for(int l=1;l<=m;l++){
					//i:倍增2的多少次方,j:哪个点往前走2^i步 k:从j能到的第k个队列中的最小值转移 l:刚才的最小值对应着五个结果
					int &tp=bei[bei[j][i-1][k]][i-1][l],&my=bei[j][i][l];
					if(rk[l][tp]<rk[l][my])my=tp;
				}
			}
		}
	}
	//cerr<<hou[1][2][1]<<" "<<hou[1][2][2]<<" "<<hou[2][4][1]<<" "<<hou[2][4][2]<<" "<<rk[1][3]<<" "<<rk[2][3]<<"\n";
	//for(int i=0;i<=4;i++){
	//	for(int j=1;j<=n;j++){
	//		for(int k=1;k<=m;k++){
	//			cerr<<j<<" "<<i<<" "<<k<<" "<<bei[j][i][k]<<"\n";
	//		}
	//	}
	//}
	int q;
	cin>>q;
	while(q--){
		int s,t;
		cin>>s>>t;
		if(s==t){
			cout<<0<<"\n";
			continue;
		}
		int ans=0;
		int now[]={0,s,s,s,s,s};
		bool fl=true;
		for(int i=1;i<=m;i++){
			if(rk[i][bei[s][18][i]]<=rk[i][s]){
				fl=false;
				break;
			}
		}
		if(fl){
			cout<<-1<<"\n";
			continue;
		}
		for(int i=17;i>=0;i--){
			int tp[6];
			memcpy(tp,now,sizeof(now));
			for(int j=1;j<=m;j++){
				for(int k=1;k<=m;k++){
					int tp2=bei[now[j]][i][k];
					if(rk[k][tp2]<rk[k][tp[k]])tp[k]=tp2;
				}
			}
			bool fl=true;
			for(int j=1;j<=m;j++){
				if(rk[j][tp[j]]<rk[j][t]){
					fl=false;
					break;
				}
			}
			if(fl)memcpy(now,tp,sizeof(tp)),ans+=(1<<i);
		}
		fl=true;
		for(int i=1;i<=m;i++){
			if(rk[i][now[i]]<rk[i][t]){
				fl=false;
				break;
			}
		}
		if(fl)cout<<ans+2<<"\n";
		else cout<<ans+1<<"\n";
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 13932kb

input:

6 2
1 3 2 5 4 6
2 1 4 3 6 5
4
1 4
5 3
6 1
5 2

output:

1
2
5
3

result:

ok 4 number(s): "1 2 5 3"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 21988kb

input:

998 5
1 2 6 3 4 5 7 11 8 9 14 12 13 10 15 16 18 19 20 17 23 22 21 26 24 25 27 29 30 28 32 31 34 33 37 38 39 35 40 36 41 43 42 46 44 48 45 47 51 49 50 56 52 57 53 54 55 62 58 59 63 64 60 65 66 61 67 69 70 68 71 73 74 75 72 78 77 79 82 76 81 85 84 80 86 87 83 88 90 91 89 92 96 98 94 95 97 101 93 103 1...

output:

74
148
1
1
1
17
1
118
180
1
15
107
21
1
1
1
64
1
1
1
1
81
1
28
79
1
1
1
21
70
1
1
73
29
29
45
1
1
1
1
1
1
1
1
15
187
137
1
1
151
67
1
1
107
1
1
61
1
46
1
1
1
3
69
1
1
88
30
74
1
1
13
115
1
1
1
102
66
125
1
21
57
162
25
140
1
197
73
1
1
1
44
139
57
19
91
93
1
98
1
1
2
1
1
1
10
60
1
64
1
61
190
97
94
...

result:

wrong answer 1st numbers differ - expected: '94', found: '74'