Question 28 (Union Sets): Polynomial Merging



Description

One day, Taotao studied polynomials in math class. The teacher gave a homework assignment: combine two polynomials containing only "+", "^" and numbers into a new polynomial.
The specific method is as follows:
There are two polynomials '2x^5+3x^3+1x^0' and '7x^6+2x^5+1x^2+7x^1', we will express them as: '3 2 5 3 3 1 0' and '4 7 6 2 5 1 2 7 1',
the first number represents how many items there are, followed by so many groups of two numbers, the first number represents the value of this item base, the second represents the exponent of this term (note: the data given in the question guarantees
that polynomial is decreasing). The polynomial after combining the two polynomials is: '7x^6+4x^5+3x*3+1x^2+7x^1+1x^0', which is expressed as: '6 7 6 4 5 3 3 1 2 7 1 1 0'
(Note: Decreasing exponent is required after merging).
Now, Taotao has handed over this task to you, and I hope you can help him complete this task (Taotao has converted the two polynomials into our expression, and we only need to use our expression to
output ).

Input

       The first line: an integer n, indicating that the first polynomial has n terms;
       the second line: n sets of integers, each set of two numbers, representing the base and exponent of each term of the first polynomial, separated by spaces.
       The third line: an integer m, indicating that the first polynomial has m terms;
       the fourth line: m groups of integers, each with two numbers, representing the base and exponent of each term of the second polynomial, separated by spaces.

Output

        The first line: an integer ans, indicating that the merged polynomial has ans terms;
       the next ans line: two numbers per line, representing the base and exponent of each term of the merged polynomial, separated by spaces.

Sample Input

 
 

Sample Input: 3 2 5 3 3 1 0 4 7 6 2 5 1 2 7 1 Sample Output: 6 7 6 4 5 3 3 1 2 7 1 1 0 Hint: 1<=n,m<=30; each item The base and exponent are both less than 99999.

AC CODE:

#include<bits/stdc++.h>
using namespace std;
int p[100000];
int n,a,b,ans=0;
int main(){
    cin>>n;
    for (int i=0;i<n;i++){
        cin>>a>>b;
        if (!p[b]) ans++;
        p[b]+=a;
    }
    cin>>n;
    for (int i=0;i<n;i++){
        cin>>a>>b;
        if (!p[b]) ans++;
        p[b]+=a;
    }
    cout<<ans<<endl;
    for (int i=99999;i>=0;i--)
    if (p[i]) cout<<p[i]<<" "<<i<<endl;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324737845&siteId=291194637