CCF CSP 202006-2 Sparse Vector (100 points in C++ language)

1. Link to the question: CCF 202006-2 Sparse Vectors

Question No.: 202006-2
Question name: sparse vector
time limit: 2.0s
Memory limit: 512.0MB
Problem Description:

2. Problem analysis:

After reading the question analysis, this question requires the calculation of two nnInner product of n- dimensional vectors under sparse representation. You only need to multiply and accumulate the values ​​of the same dimension, which is relatively simple. Three methods are given here for specific implementation: The first method uses the unordered_map container to store the first vectoruuThe mapping relationship between the u dimension and the value, and then the second vectorvvv stream processing, inO(1) O(1)O ( 1 ) time complexity Find if the sparsely represented dimension exists in the first vectoruuu , exist to do calculations. The second method, overloading the less than operator, uses the set container to automatically sort the structure composed of sparsely represented dimensions and values, and then usesO ( log ⁡ n ) O(\log n)O(logn ) Time complexity to find if the sparsely represented dimension exists in the first vectoruuu , exist to do calculations. The third method is to open a structure array composed of sparsely represented dimensions and values, overload the less than operator, and then perform a linear scan on the dimensions of two adjacent structures after sorting from small to large, and perform calculations on the same. Readers are invited to choose one as a reference and implement it by themselves.

3. C++ code program implementation method 1 - use unordered_map container:

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
int main()
{
    
    
    int n,a,b,idx,val,temp;
    scanf("%d%d%d",&n,&a,&b);
    LL ans=0;
    unordered_map<int,int> sparse_vector;
    for (int i=0;i<a ;++i )
    {
    
    
        scanf("%d%d",&idx,&val);
        sparse_vector[idx]=val;
    }
    for (int i=0;i<b ;++i )
    {
    
    
        scanf("%d%d",&idx,&val);
        temp=sparse_vector[idx];
        if (temp!=0)
        {
    
    
            ans+=val*temp;
        }
    }
    printf("%lld",ans);
    return 0;
}

4. C++ code program implementation method 2 - use set container:

#include <bits/stdc++.h>

using namespace std;
struct SparseV
{
    
    
    int index,value;
    SparseV(int _idx,int _val)
    {
    
    
        this->index=_idx;
        this->value=_val;
    }
    bool operator<(const SparseV&sv) const
    {
    
    
        return this->index<sv.index;
    }
};
int main()
{
    
    
    int n,a,b,idx,val;
    long long ans=0;
    scanf("%d%d%d",&n,&a,&b);
    set<SparseV> sparse_vector;
    for (int i=0;i<a ;++i )
    {
    
    
        scanf("%d%d",&idx,&val);
        sparse_vector.insert(SparseV(idx,val));
    }
    for (int i=0;i<b ;++i )
    {
    
    
        scanf("%d%d",&idx,&val);
        auto it=sparse_vector.find(SparseV(idx,val));
        if (it!=sparse_vector.end())
        {
    
    
            ans+=it->value*val;
        }
    }
    printf("%lld",ans);
    return 0;
}

5. C++ code program implementation method 3 - use sort to sort:

#include <bits/stdc++.h>

using namespace std;

struct sparseV
{
    
    
    int index,value;
    bool operator<(const sparseV& sv)
    {
    
    
        return this->index<sv.index;
    }
};
int main()
{
    
    
    int n,a,b,idx,val;
    long long ans=0;
    sparseV now,next;
    scanf("%d%d%d",&n,&a,&b);
    int len=a+b;
    sparseV uv[len];
    for (int i=0;i<len ;++i )
    {
    
    
        scanf("%d%d",&idx,&val);
        uv[i].index=idx;
        uv[i].value=val;
    }
    sort(uv,uv+len);
    --len;
    for (int i=0;i<len ;++i )
    {
    
    
        now=uv[i],next=uv[i+1];
        if (now.index==next.index)
        {
    
    
            ans+=now.value*next.value;
        }
    }
    printf("%lld",ans);
    return 0;
}

6. Submit AC results:

insert image description here

Guess you like

Origin blog.csdn.net/m0_46223009/article/details/125192120