P9422 [蓝桥杯 2023 国 B] 合并数列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

用队列即可
当两个队列队首:a == b ,弹出
当a < b,把a加给其后一个元素,弹出a
当b < a,把b加给其后一个元素,弹出b
#include<iostream>
#include<queue>
using namespace std;
const int N = 100010;
queue<int> a,b;
int n,m;
int main()
{
	scanf("%d %d",&n,&m);
	for(int i = 0;i < n;i ++)
	{
		int x;
		scanf("%d",&x);
		a.push(x);
	}
	for(int i = 0;i < m;i ++)
	{
		int x;
		scanf("%d",&x);
		b.push(x);
	}
	int cnt;
	while(!a.empty())
	{
		if(a.front() == b.front())
		{
			a.pop();
			b.pop();
		}
		else if(a.front() > b.front())
		{
			cnt ++;
			int t = b.front();
			b.pop();
			b.front() += t;
		}
		else
		{
			cnt ++;
			int t = a.front();
			a.pop();
			a.front() += t;
		}
	}
	printf("%d",cnt);
	return 0;
}


















