该题使用的是map容器,记录索引以及其对应值,值得注意的是,如果使用两个map容器记录,然后从0开始遍历n个数,会超时,只有70分,所以,可以在输入v第二个向量的情况时,直接进行计算向量内积,这会节省很多的时间
map<int,int> u;
map<int,int> v;
下面是代码:
#include<iostream>
#include<vector>
#include<map>
using namespace std;
int main()
{
int n,a,b;
cin>>n>>a>>b;
map<int,int> u;
// map<int,int> v;//使用1个map就够了
for(int i=0;i<a;++i)
{
int temp1;
int temp2;
cin>>temp1>>temp2;
u[temp1]=temp2;
}
long long sum=0;//int可能会不够
for(int i=0;i<b;++i)
{
int temp1,temp2;
cin>>temp1>>temp2;
if(u[temp1]!=0)//如果u向量中存在不为0的值,进行计算
sum+=temp2*u[temp1];
// v[temp1]=temp2;
}
cout<<sum<<endl;
}
下面是提交截图: