QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#377148#5116. Contestsosky123456RE 15ms426016kbC++141.7kb2024-04-04 23:01:342024-04-04 23:01:34

Judging History

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

  • [2024-04-04 23:01:34]
  • 评测
  • 测评结果:RE
  • 用时:15ms
  • 内存:426016kb
  • [2024-04-04 23:01:34]
  • 提交

answer

// Problem: P9361 [ICPC2022 Xi'an R] Contests
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P9361
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// start time: 2024-04-04 22:45:19

#include <bits/stdc++.h>
using ll = long long ;
const int maxn = 1e5+100;
int a[6][maxn] , b[6][maxn] ;
int f[6][6][30][maxn] ;
signed main() {
	std::ios::sync_with_stdio(false) ;
	std::cin.tie(nullptr);
	memset(f,1,sizeof f) ;
	int n , m ; 
	std::cin >> n >> m ;
	for(int i=1;i<=m;++i) {
		for(int j=1;j<=n;++j) {
			std::cin >> a[i][j] ;
			b[a[i][j]][i] = j ;
		}
	}  
	for(int i=1;i<=m;++i) {
		for(int j=1;j<=m;++j) {
			int min = n + 1 ; 
			for(int k = n ; k ; -- k ) {
				int pos = a[i][k] ;
				min = std::min(min,b[pos][j]) ;
				f[i][j][0][k] = min ;
			}
		}
	}
	for(int k=1;k<=20;++k) {
		for(int l=1;l<=n;++l) {
			for(int i=1;i<=m;++i) {
				for(int j=1;j<=m;++j) {
					for(int p=1;p<=m;++p) {
						f[i][p][k][l] = std::min(f[i][p][k][l],f[j][p][k-1][f[i][j][k-1][l]]);
					}
				}
			}
		}
	}
	// return 0; 
	int q; std::cin >> q;
	while(q--) {
		int x,y;std::cin >> x >> y ;
		int qwq = 0; 
		int p[m+1] ;
		for(int i=1;i<=m;++i) 
			p[i] = b[x][i] , qwq |= (b[x][i] <= b[y][i]) ;
		if(qwq) {
			std::cout << "1\n" ;
			continue ;
		}
		int num = 0 ;
		for(int j = 20 ; ~j ; --j ) {
			int np[m+1] ; 
			for(int i=1;i<=m;++i) np[i] = 2e9 ; 
			for(int i=1;i<=m;++i) 
				for(int k=1;k<=m;++k) 
					np[k] = std::min(np[k],f[i][k][j][p[i]]) ;
			int lrq = 0 ;
			for(int i=1;i<=m;++i) lrq |= (np[i] <= b[y][i]) ;
			if(!lrq) {
				num += (1<<j) ;
				for(int i=1;i<=m;++i) p[i] = np[i] ;
			}
 		}
 		if(num > 1e6) std::cout << "-1\n" ;
 		else std::cout << num+2 << '\n' ;
	}
 	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 15ms
memory: 426016kb

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
Runtime Error

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:


result: