当前位置:网站首页>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;
}
边栏推荐
- go net库(待续)
- Statistical machine learning code set
- (四)GoogleNet複現
- Recurrent+Transformer 视频恢复领域的‘德艺双馨’
- Project training of Software College of Shandong University rendering engine system radiation pre calculation (VIII)
- Multimix: small amount of supervision from medical images, interpretable multi task learning
- Why doesn't Alibaba recommend MySQL use the text type?
- 【工具推荐】个人本地 markdown 知识图谱软件 Obsidian
- [thinking about the process of structure optimization] how to build the evaluation ability of the impact of technical solutions
- 面试:什么是浅拷贝、深拷贝?
猜你喜欢

读取mhd、raw图像并切片、归一化、保存

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

Project training of Software College of Shandong University rendering engine system basic renderer (IV)

MYSQL---服务器配置相关问题

Project training of Software College of Shandong University rendering engine system radiation pre calculation (VIII)

Redis string type common commands

聊聊事件监听那些事-上

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

When programming is included in the college entrance examination...

RTOS RT thread bare metal system and multi thread system
随机推荐
Solution to idea Chinese prism garbled code error -- console Chinese output prism garbled code
acwing 2816. 判断子序列
Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]
Global and Chinese markets of bioreactors 2022-2028: Research Report on technology, participants, trends, market size and share
< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(九)
批量--03---CmdUtil
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.3 open、creat、close、unlink
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(六)
Multimix: small amount of supervision from medical images, interpretable multi task learning
Acwing794 high precision Division
从斐波那契数列求和想到的俗手、本手和妙手
5-5 configuring MySQL replication log point based replication
glibc 内存管理模型 释放 C库内存缓存
With a lamp inserted in the nostril, the IQ has risen and become popular in Silicon Valley. 30000 yuan is enough
【工具推荐】个人本地 markdown 知识图谱软件 Obsidian
面试:了解装箱和拆箱操作吗?
第一章 线性表
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.1 文件描述符
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(四)
【周赛复盘】LeetCode第80场双周赛