#1123: Good pairing time limit:1000msSingle Point time limit:1000msMemory Limit:256MBDescribe
Given two sequences A and B, each sequence may contain duplicate numbers.
A pairing (I,J) is a good match when a number AI is selected from the first sequence, a number BJ is selected from the second sequence and satisfies the AI>BJ.
Give two sequences and ask how many good pairs exist.
Input
The input contains multiple sets of data, the first line of the data is an integer t, representing the number of data groups. (t<=5)
The first row of each set of data contains two integers n and M. (0<n,m<=105)
The next n rows, two integers x and y per line, indicate that there are Y X in the first sequence.
The next m line, with two integers x and y per line, indicates that there are Y X in the second sequence. (0<x<=109,0<y<=104)
Output
For each set of data, the output is an integer that represents the number of good pairs
-
-
Sample input
-
-
12 23 24 13 12 3
-
-
Sample output
-
10
Algorithm: The complexity of O (n)
Code:
1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <math.h>5#include <iostream>6#include <string>7#include <iomanip>8#include <vector>9#include <queue>Ten#include <algorithm> One #defineN 100000+10 A - using namespacestd; - intN, M; the structNodeA - { - intx, y; - BOOL operator< (ConstNODEA&DD)Const + { - returnx<dd.x; + } A }a[n]; at - structNodeB - { - intx, y; - BOOL operator< (ConstNODEB&DD)Const - { in returnx<dd.x; - } to }b[n]; + - the intMain () * { $ intT;Panax Notoginseng intI, J; -scanf"%d", &t); the while(t--) + { Ascanf"%d%d", &n, &m); the for(i=0; i<n; i++) + { -scanf"%d%d", &a[i].x, &a[i].y); $ } $ for(i=0; i<m; i++) - { -scanf"%d%d", &b[i].x, &b[i].y); the } -Sort (A, A +n);WuyiSort (b, + +m); the Long Long intCnt=0; - Long Long intsum=0; Wuj=0; - for(i=0; i<n; i++) About { $ while(a[i].x > b[j].x && j<m) - { -cnt+=b[j].y; -J + +; A } +sum=sum+cnt*a[i].y; the //printf ("%lld---", sum); - } $printf"%lld\n", sum); the } the return 0; the}
View Code
Hihocoder Challenge 9 A. Good pairing (thinking topic prevents timeouts)