ccf 2020-06-2 稀疏向量(模拟)

思路:正常离散存储,把下标相同的对应位置乘起来相加。注意数据范围,需要使用long long
(是我的错觉吗,怎么感觉比第一个题简单)


代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<sstream>
using namespace std;
const int N = 105;
typedef long long ll;
typedef pair<ll,ll> pa;
int main()
{ 
    int n,a,b;
    cin>>n>>a>>b;
    vector<pa> v1,v2;
    for(int i=0;i<a;i++)
    {
        ll x,y;
        cin>>x>>y;
        v1.push_back({x,y});
    }
    for(int i=0;i<b;i++)
    {
        ll x,y;
        cin>>x>>y;
        v2.push_back({x,y});
    }
    ll ans=0;
    int i=0,j=0;
    while(i<a&&j<b)
    {
        if(v1[i].first==v2[j].first)
            ans+=v1[i].second*v2[j].second,i++,j++;
        else if(v1[i].first>v2[j].first)
            j++;
        else
            i++;
    }
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值