Hihocoder Challenge 9 A. Good pairing (thinking topic prevents timeouts)

Source: Internet
Author: User

#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&AMP;DD)Const +     { -         returnx<dd.x; +     } A }a[n]; at  - structNodeB - { -     intx, y; -     BOOL operator< (ConstNODEB&AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.