CSP202006-2 稀疏向量(c++)

59 阅读1分钟

CSP:202006-2 稀疏向量(c++)

在这里插入图片描述
在这里插入图片描述

#include<stdio.h>
#include<map>
using namespace std;
int main()
{
	int n, x, y;
	long long int j = 0;
	map < int, int> mp;
	scanf("%d%d%d", &n, &x, &y);
	for (int a = 0, b, c; a < x; a++)
	{
		scanf("%d%d", &b, &c);
		mp[b] = c;
	}
	map< int, int>::iterator it = mp.begin();
	for (int a = 0, b = 0, c, d; a < y; a++)
	{
		scanf("%d%d", &c, &d);
		for (; it != mp.end();)
			if (c > it->first)
				it++;
			else if (c == it->first)
			{
				j +=(it->second * d);
				break;
			}
			else break;
	}
	printf("%lld", j);
	return 0;
}

- 需注意最后输出量 j 的大小!使用int 会造成最后六个节点错误

- map的使用可极大减小储存量

  • map使用须有#include 和 using namespace std;