当前位置:网站首页>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;
}
边栏推荐
- Office VR porn, coquettish operation! The father of Microsoft hololens resigns!
- <山东大学项目实训>渲染引擎系统(八-完)
- Statistical machine learning code set
- Applet: how to get the user's mobile number in the plug-in
- Global and Chinese market of medical ECG telemetry equipment 2022-2028: Research Report on technology, participants, trends, market size and share
- Object.keys遍历一个对象
- 小飞页升级变智能修复Bug更快速了
- 同源?跨域?如何解决跨域?
- Project training of Software College of Shandong University rendering engine system basic renderer (III)
- <山东大学项目实训>渲染引擎系统(五)
猜你喜欢

The common hand, the original hand and the excellent hand from the sum of Fibonacci sequence

关于组件传值

Recurrent+Transformer 视频恢复领域的‘德艺双馨’

C packing and unpacking

Step by step to create a trial version of ABAP program containing custom screen

Multimix: small amount of supervision from medical images, interpretable multi task learning

Review of the development of China's medical beauty (medical beauty) industry in 2021: the supervision is becoming stricter, the market scale is expanding steadily, and the development prospect is bro

Batch --04--- moving components

借助SpotBugs将程序错误扼杀在摇篮中

Super detailed dry goods! Docker+pxc+haproxy build a MySQL Cluster with high availability and strong consistency
随机推荐
Saga architecture pattern: implementation of cross service transactions under microservice architecture
puppeteer入门之 BrowserContext 类
Project training of Software College of Shandong University rendering engine system basic renderer (V)
Project training of Software College of Shandong University rendering engine system radiation pre calculation (VIII)
Difference between tinyint and int
面试:hashCode()和equals()
acwing796 子矩阵的和
In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]
Learning record [email protected] understand canvas
学习记录[email protected]一文搞懂canvas
Decision tree classification and examples
同源?跨域?如何解决跨域?
Axure RP 9 for Mac(交互式产品原型设计工具)中文版
HEMA is the best representative of future retail
<山东大学项目实训>渲染引擎系统(七)
The common hand, the original hand and the excellent hand from the sum of Fibonacci sequence
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.3 open、creat、close、unlink
(四)GoogleNet複現
试用期、加班补偿———进厂前后需要了解的知识《劳动法》
Reprise de Google net