CCF 202006-2 稀疏向量

#include <iostream>
#include <bits/stdc++.h>
#include <string>

using namespace std;

typedef struct
{
    int index;
    long long int value;
}Point;

vector<Point> u,v;

bool cmp(Point a,Point b)
{
    return (a.index<b.index);
}

int main()
{
    int a,b,n;
    cin>>n>>a>>b;
    Point temp;
    while(a--)
    {
        cin>>temp.index>>temp.value;
        u.push_back(temp);
    }
    while(b--)
    {
        cin>>temp.index>>temp.value;
        v.push_back(temp);
    }
    sort(u.begin(),u.end(),cmp);sort(v.begin(),v.end(),cmp);
    vector<Point>::iterator iteru = u.begin();
    vector<Point>::iterator iterv = v.begin();
    long long int sum=0;
    while(iteru<=u.end()&&iterv<=v.end())
    {
        if((*iteru).index==(*iterv).index)
        {
            sum+=((*iteru).value)*((*iterv).value);
            iteru++;iterv++;
        }
        else if((*iteru).index<(*iterv).index)
            iteru++;
        else
            iterv++;
    }
    cout<<sum<<endl;
}
原文地址:https://www.cnblogs.com/-Asurada-/p/14372053.html