QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#659745#9412. Stop the CastleQHhee004AC ✓1ms6052kbC++172.5kb2024-10-19 21:44:492024-10-19 21:44:51

Judging History

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

  • [2024-10-19 21:44:51]
  • 评测
  • 测评结果:AC
  • 用时:1ms
  • 内存:6052kb
  • [2024-10-19 21:44:49]
  • 提交

answer

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
inline int abs(int a) {
	if(a<0)return -a;
	return a;
}
bool cmp(int i,int j) {
	return abs(i)<abs(j);
}
const int N=205*205*2;
struct Node {
	int loc,L,R;
	bool operator <(const Node &t)const {
		return L<t.L||L==t.L&&R<t.R;
	}
} r[N],c[N];
int rt,ct;
int mat[N];
int hd[N],nxt[N],to[N],tot;
bool mark[N];
void addedge(int u,int v) {
	nxt[++tot]=hd[u],to[tot]=v,hd[u]=tot;
}
bool match(int x) {
	for(int i=hd[x]; i; i=nxt[i]) {
		int y=to[i]-rt;
		if(mark[y])continue;
		mark[y]=true;
		if(!mat[y]||match(mat[y])) {
			mat[y]=x;
			return true;
		}
	}
	return false;
}
void solve() {
	int n;
	scanf("%d",&n);
	map<int,vector<int> >row,col;
	for(int i=1,u,v; i<=n; i++) {
		scanf("%d%d",&u,&v);
		row[u].push_back(v);
		col[v].push_back(u);
	}
	int m;
	scanf("%d",&m);
	for(int i=1,u,v; i<=m; i++) {
		scanf("%d%d",&u,&v);
		row[u].push_back(-v);
		col[v].push_back(-u);
	}
	rt=0,ct=0;bool flag=0;
	map<int,vector<int> > :: iterator itr1,itr2;
	for(itr1=row.begin(); itr1!=row.end(); itr1++) {
		int idx=(*itr1).first;
		vector <int> &t=(*itr1).second;
		sort(t.begin(),t.end(),cmp);
		int last=t[0];
		for(int i=1; i<t.size(); i++) {
//			printf("ridx=%d\n",idx);
			if(t[i]-last==1)flag=1;
			if(t[i]>0&&last>0)
				r[++rt]=Node{idx,last,t[i]};
			last=t[i];
		}
	}
	for(int i=1; i<=rt; i++)hd[i]=0;
	for(itr2=col.begin(); itr2!=col.end(); itr2++) {
		int idx=(*itr2).first;
		vector <int> &t=(*itr2).second;
		sort(t.begin(),t.end(),cmp);
		int last=t[0];
		for(int i=1; i<t.size(); i++) {
//			printf("cidx=%d\n",idx);
			if(t[i]-last==1)flag=1;
			if(t[i]>0&&last>0)
				c[++ct]=Node{idx,last,t[i]};
			last=t[i];
		}
	}
	if(flag) return (void)puts("-1");
	tot=0;
	for(int i=1; i<=rt; i++) {
		for(int j=1; j<=ct; j++) {
			if(c[j].L<r[i].loc&&r[i].loc<c[j].R
				&&r[i].L<c[j].loc&&c[j].loc<r[i].R)
					addedge(i,j+rt);
		}
	}
	int ans=rt+ct;
	for(int i=1; i<=ct; i++)mat[i]=0;
	for(int i=1; i<=rt; i++) {
		for(int j=1; j<=ct; j++)mark[j]=false;
		if(match(i))ans--;
	}
	printf("%d\n",ans);
	for(int i=1; i<=rt; i++)mark[i]=false;
	for(int i=1,x,y; i<=ct; i++) {
		if(!mat[i]) {
			x=c[i].L+1,y=c[i].loc;
		} else {
			x=r[mat[i]].loc,y=c[i].loc;
			mark[mat[i]]=true;
		}
		printf("%d %d\n",x,y);
	}
	for(int i=1; i<=rt; i++) {
		if(mark[i])continue;
		printf("%d %d\n",r[i].loc,r[i].L+1);
	}
}
int main() {
	int T;scanf("%d",&T);
	while(T--)solve();
	return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5812kb

input:

4
7
1 3
6 6
4 7
2 1
6 3
4 1
2 6
3
3 4
6 4
3 1
2
1 1
2 2
0
3
1 1
1 3
3 3
1
1 2
3
1 1
1 3
2 3
0

output:

2
2 3
4 6
0
1
2 3
-1

result:

ok ok 4 cases (4 test cases)

Test #2:

score: 0
Accepted
time: 0ms
memory: 6052kb

input:

21
11
3 5
18 6
19 12
27 48
28 38
30 12
33 18
42 18
43 38
45 46
48 34
3
2 6
24 4
41 45
15
11 6
15 27
16 9
16 14
16 48
19 26
25 12
27 26
28 4
31 40
32 6
33 50
37 50
46 11
50 29
17
1 49
4 9
9 15
9 22
11 31
11 46
14 28
17 5
17 49
18 43
20 31
30 46
33 11
33 33
34 15
36 6
47 10
2
16 46
36 30
11
7 34
7 41
...

output:

3
20 12
34 18
29 38
5
12 6
20 26
34 50
16 10
16 15
0
1
16 10
0
1
43 22
5
42 44
1 3
1 13
33 10
44 45
0
5
8 1
21 15
27 41
29 26
44 4
1
32 9
0
0
0
0
6
23 10
35 34
24 46
12 11
23 44
29 21
0
3
31 17
20 30
43 25
0
-1
3
16 36
17 39
25 7
6
5 5
4 9
3 10
8 11
6 4
7 3

result:

ok ok 21 cases (21 test cases)

Test #3:

score: 0
Accepted
time: 1ms
memory: 4068kb

input:

2
200
7 52
9 160
10 4
12 61
18 436
19 430
20 499
24 139
25 416
29 53
33 153
35 275
35 310
37 25
38 477
39 349
42 120
44 158
45 330
49 486
50 204
51 67
52 138
52 305
56 139
63 132
66 4
67 327
70 484
71 67
72 308
74 218
76 479
81 139
82 90
86 201
87 335
91 35
91 220
92 496
94 29
94 436
96 359
97 299
1...

output:

46
11 4
38 25
94 35
240 35
91 61
312 61
52 67
126 67
349 112
154 138
52 139
57 139
185 147
224 153
132 160
311 177
271 186
352 186
278 272
126 275
270 305
415 305
393 307
277 356
286 367
334 367
306 374
380 385
138 429
199 432
19 436
187 467
39 477
154 496
21 499
35 276
187 433
224 393
260 223
261 4...

result:

ok ok 2 cases (2 test cases)

Test #4:

score: 0
Accepted
time: 0ms
memory: 4084kb

input:

20
13
89287395 441913071
89287395 590314987
142917372 582720391
142917372 598813561
232930851 582720391
263974835 468188689
263974835 490702144
543529670 152471388
543529670 219371706
647691663 598813561
772865038 598813561
773363571 482933265
773363571 561453301
8
128947560 120560639
264980960 4742...

output:

8
142917373 582720391
142917373 598813561
647691664 598813561
89287395 441913072
142917372 582720392
263974835 468188690
543529670 152471389
773363571 482933266
7
808969360 354711322
92745430 358274214
469117951 762966373
808969359 818037531
59832704 871951216
808969359 386523246
808969359 891229782...

result:

ok ok 20 cases (20 test cases)

Test #5:

score: 0
Accepted
time: 1ms
memory: 5844kb

input:

2
184
35763790 435860426
152681166 443483111
261932524 745277126
261932524 787982418
314305867 342102981
314305867 522593781
314305867 747023891
314305867 811404535
314305867 816914009
314305867 857896659
319901983 797458033
321347896 342102981
332550928 902179526
343207116 914408792
350635050 15515...

output:

160
469496530 154041437
362269862 159320601
433090048 271588697
702209411 328140561
314305868 342102981
321347897 342102981
350635051 342102981
407380694 342102981
469496529 342102981
556513262 342102981
561219908 342102981
593934228 342102981
611662527 342102981
620290597 342102981
638591122 342102...

result:

ok ok 2 cases (2 test cases)

Test #6:

score: 0
Accepted
time: 1ms
memory: 5792kb

input:

2
193
78368109 329329995
87932514 657474465
87932514 903492853
94608574 845872655
103602204 88649154
124431127 405860533
145262472 108198774
145262472 907127202
154400439 136367504
165270984 773451933
184157521 409828915
197728547 291882089
197728547 609623645
211750768 843867422
246178069 108396139...

output:

145
365329222 29599937
367267913 29599937
402471584 29599937
411683746 29599937
412106896 29599937
365329222 62966785
103602205 88649154
365329222 88649154
412106896 88649154
457396765 88649154
515322528 88649154
412106896 108887129
365329222 135814914
412106895 135814914
421345803 135814914
3653292...

result:

ok ok 2 cases (2 test cases)

Test #7:

score: 0
Accepted
time: 0ms
memory: 5784kb

input:

2
192
1075648 579875717
1075648 937648715
1393060 191616432
3957616 541362608
3957616 971879257
5415801 541362608
5887448 541362608
5887448 624842533
25767120 610618164
26193206 214214028
26193206 231445627
26727662 374314271
29550509 755417826
29550509 917592925
30033126 610618164
31134588 58188305...

output:

131
314103357 11543659
145434751 34712108
622701955 58188305
31134588 191616432
597642622 194741814
746663436 213352628
31134588 231445627
38104744 231445627
133595744 231445627
228736624 231445627
235369724 231445627
250896471 231445627
306167345 231445627
341366249 231445627
417168695 231445627
41...

result:

ok ok 2 cases (2 test cases)

Test #8:

score: 0
Accepted
time: 1ms
memory: 5852kb

input:

20
14
378192988 147499872
378192988 837331700
449924898 147499872
449924898 837331700
592684636 147499872
592684636 837331700
783332532 147499872
783332532 837331700
378192988 197547597
783332532 197547597
378192988 343857831
783332532 343857831
378192988 576579017
783332532 576579017
2
449924898 19...

output:

15
378192989 147499872
449924899 147499872
592684637 147499872
378192989 576579017
378192989 837331700
449924899 837331700
592684637 837331700
378192988 147499873
378192988 197547598
378192988 343857832
378192988 576579018
783332532 147499873
783332532 197547598
783332532 343857832
783332532 5765790...

result:

ok ok 20 cases (20 test cases)

Test #9:

score: 0
Accepted
time: 1ms
memory: 5776kb

input:

2
200
8623893 3649468
8623893 989134714
24820918 3649468
24820918 989134714
43705451 3649468
43705451 989134714
45500146 3649468
45500146 989134714
58265855 3649468
58265855 989134714
112263159 3649468
112263159 989134714
126496650 3649468
126496650 989134714
137574156 3649468
137574156 989134714
14...

output:

249
8623894 3649468
24820919 3649468
43705452 3649468
45500147 3649468
58265856 3649468
112263160 3649468
126496651 3649468
137574157 3649468
140089446 3649468
156036790 3649468
170794246 3649468
176576710 3649468
186774360 3649468
194114591 3649468
222368049 3649468
237249113 3649468
250839458 3649...

result:

ok ok 2 cases (2 test cases)

Test #10:

score: 0
Accepted
time: 1ms
memory: 3820kb

input:

2
200
2691939 27674656
2691939 984614319
30225807 27674656
30225807 984614319
39388076 27674656
39388076 984614319
55883389 27674656
55883389 984614319
77258752 27674656
77258752 984614319
106874917 27674656
106874917 984614319
144383292 27674656
144383292 984614319
145360776 27674656
145360776 9846...

output:

216
2691940 27674656
30225808 27674656
39388077 27674656
55883390 27674656
77258753 27674656
106874918 27674656
144383293 27674656
145360777 27674656
149992531 27674656
161288469 27674656
189142971 27674656
228097973 27674656
247597873 27674656
258196659 27674656
279200012 27674656
285200937 2767465...

result:

ok ok 2 cases (2 test cases)

Test #11:

score: 0
Accepted
time: 0ms
memory: 3748kb

input:

2
200
1005937 7
3412691 7
5764908 7
7897785 7
8061992 7
12801501 7
18529696 7
22574605 7
25203351 7
34574958 7
34998216 7
40928451 7
49792193 7
55962320 7
59632864 7
62353373 7
62611196 7
65346591 7
71459320 7
71550121 7
72215989 7
82584263 7
86119549 7
96204862 7
96940713 7
97693112 7
102067050 7
1...

output:

199
1005938 7
3412692 7
5764909 7
7897786 7
8061993 7
12801502 7
18529697 7
22574606 7
25203352 7
34574959 7
34998217 7
40928452 7
49792194 7
55962321 7
59632865 7
62353374 7
62611197 7
65346592 7
71459321 7
71550122 7
72215990 7
82584264 7
86119550 7
96204863 7
96940714 7
97693113 7
102067051 7
105...

result:

ok ok 2 cases (2 test cases)

Extra Test:

score: 0
Extra Test Passed