当前位置:网站首页>acwing 800. 数组元素的目标和
acwing 800. 数组元素的目标和
2022-06-12 16:08:00 【_刘小雨】
给定两个升序排序的有序数组 A 和 B,以及一个目标值 x。
数组下标从 0 开始。
请你求出满足 A[i]+B[j]=x 的数对 (i,j)。
数据保证有唯一解。
输入格式
第一行包含三个整数 n,m,x,分别表示 A 的长度,B 的长度以及目标值 x。
第二行包含 n 个整数,表示数组 A。
第三行包含 m 个整数,表示数组 B。
输出格式
共一行,包含两个整数 i 和 j。
数据范围
数组长度不超过 105。
同一数组内元素各不相同。
1≤数组元素≤109
输入样例:
4 5 6
1 2 4 7
3 4 6 8 9
输出样例:
1 1
方法1: 直接想到暴力求解 O(n2)
for(int i =0; i < n; i++)
for(int j = 0; j < m; j ++)
if(a[i] + b[j] == x) cout << xxx;
利用双指针进行优化: O(n) 利用了单调性
图解:
思考: 这里为什么是O(n)
for(int i =0; i < n; i++)
while(j >=0 && a[i] + b[j] > x) j --;
这里j 只是回退了,它在指针i移动时,并不会回到最后重新开始。
思想:
为了减少循环,采用了两个指针来保证能遍历符合要求的所有数;
i指针从A数组的头开始,此时a[i]最小;
j指针从B数组的尾开始,此时b[j]最大;
在i = 0 时,判断a[i] + b[j] 的值,如果大于x,j- -;
也就是说b[j]的值一直在减小;
那么当a[i] + b[j] 的值第一次小于x时,j指针的左边的数与a[i]相加一定都小于x了;那么我们只能增大a[i]的值;
于是i ++;
自此开始反复循环,如果a[i] + b[j] 大于x,就减小b[j](相当于将j指针左移)反之,就增大a[i](相当于i指针右移)
这样,一定保证了每一个符合要求的两个数都能加一遍,判断是不是和等于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;
}
边栏推荐
- Project training of Software College of Shandong University rendering engine system point cloud processing (10)
- [browser principle] variable promotion
- Example of bit operation (to be continued)
- Install RHEL 7/8 (red hat) virtual machine (Reprint)
- Scanpy(六)空间转录组数据的分析与可视化
- Codeworks round 797 (Div. 3, cf1690)
- Solutions to some problems of scuacm22 retreat competition before summer training
- Escape analysis of golang compiler
- CUDA out of memory or brokenpipeerror: [errno 32] broken pipe or oserror: [winerror 1455] solution to the problem that the page file is too small
- 写代码也有本手俗手之分,而我们要善于发现妙手!
猜你喜欢

Five models of software testing

超详细干货!Docker+PXC+Haproxy搭建高可用强一致性的MySQL集群
![Analysis of global and Chinese shipbuilding market in 2021: the global shipbuilding new orders reached 119.85 million dwt, with China, Japan and South Korea accounting for 96.58%[figure]](/img/3e/b54b7f15c4a6326d8c7c4433388a3a.jpg)
Analysis of global and Chinese shipbuilding market in 2021: the global shipbuilding new orders reached 119.85 million dwt, with China, Japan and South Korea accounting for 96.58%[figure]

同源?跨域?如何解决跨域?

< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(四)
![Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]](/img/48/714f1712f4c2d727dd49cbc6631abf.jpg)
Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]

小飞页升级变智能修复Bug更快速了

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

Redis General Command

How to use grafana to easily realize OVL data visualization
随机推荐
PHP builds a high-performance API architecture based on sw-x framework (II)
< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(九)
Unity get local video / download network video
Global and Chinese markets for air sampling calibration pumps 2022-2028: Research Report on technology, participants, trends, market size and share
盒马,最能代表未来的零售
Fiddler packet capturing (mobile app)
【周赛复盘】LeetCode第80场双周赛
任务 水果炸汁机 0611
一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤试读版
小飞页升级变智能修复Bug更快速了
Match single character
[automation] kolla Based Automated Deployment CEPH cluster
从斐波那契数列求和想到的俗手、本手和妙手
第一章 线性表
Project training of Software College of Shandong University rendering engine system basic renderer (6)
作业提交说明 上传作业到网盘中
Global and Chinese market of soft capsule manufacturing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
C语言 分割bin文件程序
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(六)
【工具推荐】个人本地 markdown 知识图谱软件 Obsidian