CSP-202006-2-sparse vector

Sparse vector (portal)

I don't know why, the four arrays used will report a running error, only 60 points, and then I read a vector<pair<int, int>> or map written by a big guy and got full marks after submitting it.

Note the scope: the result uses long long type

Sample input

10 3 4
4 5
7 -3
10 1
1 10
4 20
5 30
7 40

Sample output

-20

Full score code

#include <bits/stdc++.h>

using namespace std;

int n,a,b;
long long sum;

vector<pair<int, int> > u, v;

int main(){
    
    
	
	cin >> n >> a >> b;
	
	for(int i = 0; i < a; i++) {
    
    
		int x,y;
		cin >> x >> y;
		u.push_back({
    
    x,y});
	}
	
	for(int i = 0; i < b; i++) {
    
    
		int x,y;
		cin >> x >> y;
		v.push_back({
    
    x,y});
	}
	
	int i = 0, j = 0;
	
	while(i < a && j < b) {
    
    
		if(u[i].first == v[j].first) {
    
    
			sum += u[i++].second*v[j++].second;
		}else if(u[i].first > v[j].first) {
    
    
			j++;
		}else {
    
    
			i++;
		}
	}
	
	cout << sum << endl;
	
	return 0;
}

Here is the title O(∩_∩)O, everyone is welcome to leave a message, if you have time, you can like it (#^. ^#)

       Question number:
202006-2
Question name: Sparse vector
time limit: 1.0s
Memory limit: 512.0MB
Problem Description: Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44635198/article/details/108422063