Problem description: N binary groups (OI, Ti) are given to see if they can be matched one by one, that is, (OI, Ti) --- (Ti, oi). Yes, no
Example:
3
1 2
2 1
1 2
No
4
1 2
2 1
1 2
2 1
Yes
Type: Sort
Experience: This question is a good question. I have to use points to do it myself ,,.... Experience: Just do it. Sometimes, don't worry too much ,...
In addition, I wrote a qsort for this question and found many problems. I had always been very careless about the = in CMP. It is best to use 0 to return =, avoid errors
Solution: sort the starting point Oi and Ti by the order of OI and Ti as the main keywords, and then compare whether each of the two arrays has a one-to-one correspondence. For example
3
1 2
2 1
1 2
Take Oi as the keyword, (1, 2), (1, 2), (2, 1)
Take Ti as the keyword, (2, 1), (1, 2), (1, 2)
If one-to-one matching is required, the OI of the second array = Ti of the first array, and the Ti of the second array must be the oi of the first array. Obviously, the above example is not matched,
4
1 2
2 1
1 2
2 1
Keyword with Oi, (1, 2), (1, 2), (2, 1), (2, 1)
Keywords WITH Ti)
# Include <cstdio> <br/> # include <cstdlib> <br/> using namespace STD; <br/> const int max = 500000 + 10; <br/> int Order1 [Max], order2 [Max], O [Max], t [Max]; <br/> void swap (Int & A, Int & B) <br/> {int temp = A; A = B; B = temp ;}< br/> int N; <br/> int CMP1 (int A, int B) <br/>{</P> <p> If (O [a] <o [B]) Return-1; <br/> else if (O [a]> O [B]) return 1; <br/> If (T [a] <t [B]) Return-1; <br/> else if (T [a] = T [B]) return 0; <br/> else return 1; <br/>}< br/> int cmp2 (int A, int B) <br/> {<br/> If (T [a] <t [B]) Return-1; <br/> else if (T [a]> T [B]) return 1; <br/> If (O [a] <o [B]) Return-1; <br/> else if (O [a] = O [B]) return 0; <br/> else return 1; <br/>}< br/> // int (* CMP) (INT, INT); <br/> int partition (int * a, int X, int y, INT (* CMP) (INT, INT) <br/>{< br/> int P = x, q = Y, K = A [x]; <br/> // CMP = comp; <br/> while (1) <br/>{< br/> // printf ("$ # $ % d/N", [P + 1]); <br/> while (CMP (A [+ + P], k) <0 & P <Y-1 ); <br/> while (CMP (A [-- q], k)> 0 & q> X ); <br/> // printf ("$ % d/N", q); <br/> If (P> = q) break; <br/> swap (A [p], a [Q]); <br/>}< br/> A [x] = A [Q]; <br/> A [Q] = K; </P> <p> return Q; </P> <p >}< br/> void mqsort (int *, int X, int y, INT (* CMP) (INT, INT) <br/>{< br/> // CMP = comp; <br/> If (Y-x> 1) <br/>{< br/> int q = partition (A, X, Y, CMP ); <br/> mqsort (A, X, Q, CMP); <Br/> mqsort (A, q + 1, Y, CMP); <br/>}< br/> int main () <br/>{</P> <p> while (scanf ("% d", & n) = 1 & N) <br/> {<br/> for (INT I = 0; I <n; I ++) <br/> {<br/> int A, B; <br/> scanf ("% d", & O [I], & T [I]); <br/>}< br/> for (INT I = 0; I <n; I ++) <br/> {Order1 [I] = I; order2 [I] = I ;}</P> <p> mqsort (Order1, 0, N, CMP1); <br/> mqsort (order2, 0, n, cmp2); <br/> int OK = 1; <br/> for (INT I = 0; I <n; I ++) <br/> {<br/> // printf ("% d/ N ", Order1 [I], order2 [I]); <br/> If (O [Order1 [I]! = T [order2 [I] | T [Order1 [I]! = O [order2 [I]) {OK = 0; break ;}</P> <p >}< br/> If (OK) printf ("Yes/N"); <br/> else printf ("No/N "); </P> <p >}< br/> // system ("pause"); <br/> return 0; <br/>}< br/>