当前位置:网站首页>acwing 800. Target and of array elements
acwing 800. Target and of array elements
2022-06-12 16:18:00 【_ Liuxiaoyu】
Given two ordered arrays sorted in ascending order A and B, And a target value x.
Array index from 0 Start .
Please find satisfaction A[i]+B[j]=x The number of (i,j).
Data guarantees a unique solution .
Input format
The first line contains three integers n,m,x, respectively A The length of ,B The length and target value of x.
The second line contains n It's an integer , Represents an array A.
The third line contains m It's an integer , Represents an array B.
Output format
All in one line , Contains two integers i and j.
Data range
Array length not more than 105.
The elements in the same array are different .
1≤ Array elements ≤109
sample input :
4 5 6
1 2 4 7
3 4 6 8 9
sample output :
1 1
Method 1: Think of violence directly O(n2)
for(int i =0; i < n; i++)
for(int j = 0; j < m; j ++)
if(a[i] + b[j] == x) cout << xxx;
Optimize with double pointers : O(n) Using monotonicity
The illustration :
reflection : Why is this O(n)
for(int i =0; i < n; i++)
while(j >=0 && a[i] + b[j] > x) j --;
here j Just back off , It's in the pointer i When moving , It won't go back to the end and start again .
thought :
To reduce circulation , Two pointers are used to ensure that all numbers that meet the requirements can be traversed ;
i Pointer from A Start with the header of the array , here a[i] Minimum ;
j Pointer from B Start at the end of the array , here b[j] Maximum ;
stay i = 0 when , Judge a[i] + b[j] Value , If it is greater than x,j- -;
in other words b[j] The value of has been decreasing ;
So when a[i] + b[j] The value of is less than... For the first time x when ,j The number to the left of the pointer is the same as a[i] The sum must be less than x 了 ; Then we can only increase a[i] Value ;
therefore i ++;
Since then, it has been repeated , If a[i] + b[j] Greater than x, Just reduce b[j]( Is equivalent to j Move the pointer to the left ) conversely , Just increase a[i]( amount to i Move the pointer to the right. )
such , It must be ensured that every two numbers that meet the requirements can be added again , Judge whether sum equals x.
code:
#include <iostream>
using namespace std;
const int N = 100010;
int n,m,k;
int a[N], b[N];
int main()
{
cin >> n >> m >> k;
for(int i = 0; i < n; i ++) cin >> a[i];
for(int i = 0; i < m; i ++) cin >> b[i];
for(int i = 0, j = m -1; i < n; i ++)
{
while(j >=0 && a[i] + b[j] > k ) j --;
if(k == a[i] + b[j])
{
cout << i << ' ' << j << endl;
}
}
return 0;
}
边栏推荐
- Axure RP 9 for Mac(交互式产品原型设计工具)中文版
- C packing and unpacking
- Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]
- In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]
- 记一篇IT培训日记067-好人感恩,坏人无错
- Recurrent+Transformer 视频恢复领域的‘德艺双馨’
- Keep an IT training diary 067- good people are grateful, bad people are right
- 写代码也有本手俗手之分,而我们要善于发现妙手!
- Global and Chinese markets of bioreactors 2022-2028: Research Report on technology, participants, trends, market size and share
- Redis string type common commands
猜你喜欢

Huawei equipment is configured with CE dual attribution

acwing 790. 数的三次方根(浮点数二分)

< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(六)

Sum of acwing796 submatrix

acwing 803. Interval merging
![In 2020, the demand for strain sensors in China will reach 9.006 million, and the market scale will reach 2.292 billion yuan [figure]](/img/a8/dd5f79262fe6196dd44ba416a4baac.jpg)
In 2020, the demand for strain sensors in China will reach 9.006 million, and the market scale will reach 2.292 billion yuan [figure]

办公室VR黄片,骚操作!微软HoloLens之父辞职!

< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(九)

With a lamp inserted in the nostril, the IQ has risen and become popular in Silicon Valley. 30000 yuan is enough

关于组件传值
随机推荐
Multimix:从医学图像中进行的少量监督,可解释的多任务学习
办公室VR黄片,骚操作!微软HoloLens之父辞职!
连续八年包装饮用水市占率第一,这个品牌DTC是如何持续增长的?
The C Programming Language(第 2 版) 笔记 / 7 输入与输出 / 7.8 其它函数
With a lamp inserted in the nostril, the IQ has risen and become popular in Silicon Valley. 30000 yuan is enough
同源?跨域?如何解决跨域?
d的sha6转大整
学习记录[email protected]一文搞懂canvas
一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤试读版
<山东大学项目实训>渲染引擎系统(八-完)
<山东大学项目实训>渲染引擎系统(五)
Global and Chinese markets of bioreactors 2022-2028: Research Report on technology, participants, trends, market size and share
Recurrent+Transformer 视频恢复领域的‘德艺双馨’
面试:为什么整数包装类尽量用equals()来比较大小
acwing 798二维差分(差分矩阵)
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.6 实例(目录列表)
聊聊事件监听那些事-上
From K-means to capsule
Acwing 797 differential
Kill program errors in the cradle with spotbugs