ccf 202006-2 稀疏向量 python满分代码

ccf 202006-2 稀疏向量 python满分代码

参考链接:https://blog.csdn.net/weixin_41480156/article/details/107701647
9.13更新:好奇怪,第一种之前提交还是满分,这两天再试又运行超时了,不知道哪里出了问题。

import sys


def input2num():
    return map(int, input().split())


dim, cap_a, cap_b = input2num()
a = {}
tol = 0

for line in sys.stdin.readlines():
    i, n = map(int, line.split())
    if cap_a > 0:
        a[i] = n
        cap_a -= 1
    else:
        if i in a:
            tol += n * a[i]

print(tol)
第二种方法在评论区:https://blog.csdn.net/weixin_41480156/article/details/107701647
python满分代码,靠谱!

在这里插入图片描述

import sys
n, a, b = tuple(map(int,input().split()))
vector = list(map(int,sys.stdin.read().split()))
i = 0
j = 0
s = 0
while i<a and j<b:
    if vector[i*2] == vector[(a+j)*2]:
        s += vector[i*2+1]*vector[(a+j)*2+1]
        i+=1
        j+=1
    elif vector[i*2] < vector[(a+j)*2]:
        i+=1
    else:
        j+=1
print(s)
附上C++满分代码:https://blog.csdn.net/wingrez/article/details/107588745

在这里插入图片描述

#include<cstdio>
#include<vector>
using namespace std;

int n;
int a, b;
long long ans; // 使用 long long  

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

int main() {
	scanf("%d%d%d", &n, &a, &b);
	int id, val;
	for(int i=0; i<a; i++) {
		scanf("%d%d", &id, &val);
		u.push_back({id, val});
	}
	
	for(int i=0; i<b; i++) {
		scanf("%d%d", &id, &val);
		v.push_back({id, val});
	}
	
	int i=0, j=0;
	while(i<a && j<b){
		if(u[i].first==v[j].first){
			ans += u[i].second * v[j].second;
			i++; j++;
		}
		else if(u[i].first>v[j].first){
			j++;
		}
		else{
			i++;
		}
	}

	printf("%lld\n", ans);

	return 0;
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值