The 14th Lanqiao Cup Competition Software Competition Final C/C++ University Group B Test Question D: Merging Sequences

[Lanqiao Cup 2023 Country B] Merged Sequence

【Problem Description】

Xiao Ming found that there are many ways to split a large positive integer into the sum of several positive integers. He adopted two of the solutions and listed them as two arrays { a 1 , a 2 , ⋯ an } \{a_1, a_2, \cdots a_n\}{ a1,a2,an} { b 1 , b 2 , ⋯ b m } \{b_1, b_2, \cdots b_m\} { b1,b2,bm} . The sum of both arrays is the same.

Defining a merge operation can merge two adjacent numbers in an array into a new number. The value of the new number is the sum of the original two numbers. Xiao Ming wants to make the two arrays exactly the same through several merge operations, that is, n = mn = mn=m and for any subscriptiii full footai = bi a_i = b_iai=bi. Please calculate the minimum number of merge operations required to complete Xiao Ming's goal.

【Input format】

Enter a total of 3 33 lines.
The first line is two positive integersn, mn, mn,m .
The second lineis nn integers a 1 , a 2 , ⋯ , an a_1, a_2, \cdots, a_nseparated by spacesa1,a2,,an.
The third line is mmm integers b 1 , b 2 , ⋯ , bm b_1, b_2, \cdots, b_mseparated by spacesb1,b2,,bm

Output format

Output total 1 11 line, an integer.

[Sample input]

4 3
1 2 3 4
1 5 4

[Sample output]

1

[Sample description]

Just change a 2 a_2a2Japanese a 3 a_3a3Merge, array aaa becomes{ 1 , 5 , 4 } \{1,5,4\}{ 1,5,4 } , that is, andbbb is the same.

[Evaluation use case scale and agreement]

  • For 20% 20\%20% of the data ensuresn, m ≤ 1 0 3 n,m \le 10^3n,m103
  • For 100 % 100\%100% data, guaranteedn, m ≤ 1 0 5 n, m \le 10^5n,m105 0 < a i , b i ≤ 1 0 5 0 < a_i, b_i \le 10^5 0<ai,bi105
#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	list<int> a,b;
	int n,m;cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
    
    
		int x;cin>>x;
		a.push_back(x);
	}
	for(int i=1;i<=m;i++)
	{
    
    
		int x;cin>>x;
		b.push_back(x);
	}
	int cnt=0;
	while(!a.empty() && !b.empty())
	{
    
    
		if(a.front()==b.front()) 
		{
    
    
			a.pop_front();
			b.pop_front();
		}
		else if(a.front()<b.front())
		{
    
    
			int a1=a.front();a.pop_front();
			int a2=a.front();a.pop_front();
			a.push_front(a1+a2);
			cnt++;
		}
		else if(a.front()>b.front())
		{
    
    
			int b1=b.front();b.pop_front();
			int b2=b.front();b.pop_front();
			b.push_front(b1+b2);
			cnt++;
		}
	}
	cout<<cnt;
	
    return 0;
}

Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/qq_52792570/article/details/133434000