题解分享
题解分享简介
题解
看到大家都队列模拟,但前缀和也可以吧。代码如下:
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
typedef long long ll;
ll a[N], b[N];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
ll tmp;
cin >> tmp;
a[i] = a[i - 1] + tmp;
}
for (int i = 1; i <= m; i++)
{
ll tmp;
cin >> tmp;
b[i] = b[i - 1] + tmp;
}
ll res = 0;
int index1 = 1, index2 = 1;
int pre_index1 = 0, pre_index2 = 0;
while (index1 <= n && index2 <= m)
{
if (a[index1] - a[pre_index1] == b[index2] - b[pre_index2])
{
res += index1 + index2 - pre_index1 - pre_index2 - 2;
pre_index1 = index1;
pre_index2 = index2;
index2++;
index1++;
}
else if (a[index1] - a[pre_index1] < b[index2] - b[pre_index2])
{
index1++;
}
else
{
index2++;
}
}
cout << res;
return 0;
}
```
查看全文
1
0
0
6
合并数列(编程题) - 题解
emmmm既然要最后两个数组一样 那么他们的长度必须一样,而且只能有合并操作,那么合并一次长度减一,在第二个数组不进行合并的情况下,直接用两个数组长度相减就是答案。而且由题目可以得到,第二个数组不进行任何操作的情况也是正解
```
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef struct point{
int x;
int y;
}point;
int main(){
int n,m;
cin>>n>>m;
int arr[n+10];
int brr[m+10];
for(int i=1;i<=n;i++)
cin>>arr[i];
for(int j=1;j<=m;j++)
cin>>brr[j];
cout<<abs(n-m)<<endl;
return 0;
}
```
查看全文
8
0
0
55
合并数列(编程题) - 题解
直接模拟即可, 代码很简单, 你一定能看明白的
```cpp
#include <cstdio>
#include <list>
using namespace std;
int main() {
int res = 0;
int n = 0, m = 0;
scanf("%d %d", &n, &m);
list<int> arr;
list<int> brr;
for (int i = 0, j; i < n; ++i) {
scanf("%d", &j);
arr.push_back(j);
}
for (int i = 0, j; i < m; ++i) {
scanf("%d", &j);
brr.push_back(j);
}
while (arr.size() && brr.size()) {
if (*arr.rbegin() == *brr.rbegin()) {
arr.pop_back();
brr.pop_back();
} else {
++res;
if (*arr.rbegin() > *brr.rbegin()) {
// 合并 brr 的
int x = *brr.rbegin();
brr.pop_back();
int& y = *brr.rbegin();
y += x;
} else {
// 合并 arr 的
int x = *arr.rbegin();
arr.pop_back();
int& y = *arr.rbegin();
y += x;
}
}
}
printf("%d\n", res);
return 0;
}
```
查看全文
6
0
1
6
合并数列(编程题) - 题解
```
#include <bits/stdc++.h>
using namespace std;
queue<int> a, b;
signed main(){
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; ++i){
int x; cin >> x;
a.push(x);
}
for(int i = 1; i <= m; ++i){
int x; cin >> x;
b.push(x);
}
int cnt = 0;
while(a.size() || b.size()){
int top1 = a.front(), top2 = b.front();
a.pop(), b.pop();
while(top1 != top2){
if(top1 < top2){
top1 += a.front();
++cnt;
a.pop();
}else{
top2 += b.front();
++cnt;
b.pop();
}
}
}
cout << cnt << endl;
return 0;
}
```
查看全文
3
0
0
26
合并数列(编程题) - 题解
include
define int long long
using namespace std;
int n,m,ans=0;
int a[100001],b[100001];
```
`
```
````
signed main()
{
cin >>n>>m;
for(int i=0;i
b[j])//同理
{
j++;
ans++;
if(j<m)
b[j]+=b[j-1];
}
}
cout <<ans<<endl;
return 0;
}
查看全文
1
0
0
35



