AcWing 1532. Find coins [Detailed explanation of dual pointer algorithm]

topic

Eva likes to collect coins from the entire universe.

One day, she went to a universe shopping mall to shop, and she could use various coins to pay at the checkout.

However, there is a special payment requirement: for each bill, she can only use exactly two coins to pay the consumption amount accurately.

Given the denominations of all the coins she has, please help her determine whether she can find two coins to pay for the given amount.

Input format The
first line contains two integers N and M, which respectively represent the number of coins and the amount to be paid.

The second line contains N integers, representing the denomination of each coin.

Output format
Output one line, containing two integers V1, V2, representing the denominations of the two selected coins, such that V1≤V2 and V1+V2=M.

If the answer is not unique, the solution with the smallest V1 is output.

If there is no solution, output No Solution.

Data range
1≤N≤105,
1≤M≤1000
Input example 1:
8 15
1 2 8 7 2 4 11 15
Output example 1:
4 11
Input example 2:
7 14
1 8 7 2 4 11 15
Output Sample 2:
No Solution

What is a dual pointer algorithm?

Introduction The
dual pointer algorithm is widely used, and it can be used as a more efficient algorithm because it fixes some order for combination items and directly excludes some combination options compared with ordinary brute force search. The idea is that each of the two pointers, one pointer is responsible for looping, and the other pointer is responsible for checking conditions and cooperating.
template

for (int i = 0, j = 0; i < n; i ++ )
{
    
    
    while (j < i && check(i, j)) j ++ ;

    // 具体问题的逻辑
}
常见问题分类:
    (1) 对于一个序列,用两个指针维护一段区间
    (2) 对于两个序列,维护某种次序,比如归并排序中合并两个有序序列的操作

What are the conditions of use?

Where the two-pointer algorithm can be used, it can be solved violently. We first come up with a violent solution, and then consider using the two-pointer algorithm for optimization. If dual pointers can be used to maintain an interval, then this interval must be monotonic . Because only when monotonicity is satisfied, we can fix some order for the combination items and directly exclude other combination options.

Problem solving ideas

For this problem, first come up with a violent solution.

for (int i = 0; i < n; i++)
{
    
    
	for (int j = 0; j < i; j++)
	{
    
    
		if (a[i] + a[j] == m) //更新答案
		{
    
    
		
		}
	}
}

The time complexity is O(n^2)very easy out.

Consider dual pointer algorithm optimization

Sort the array first to make it orderly .

Then define two pointers i, j. iPoint to the beginning of the array, jpoint to the end of the array.
Insert picture description here

When the two pointer does not intersect, and a[i]+a[j]>mdescription a[j]taken large, moves to the left jpointer, and so the number of two a[i] +a[j]is reduced. That execution while (i<j&&a[i] + a[j]>m) j--;until a[i]+a[j]<=mexiting the loop.

Then judgment a[i] +a[j]is equal to m, if not equal to the description a[i]taken smaller, move ithe pointer, so that two numbers a[i] +a[j]increase.

Continue until the above operation a[i] +a[j] ==m, we record the answers.

	sort(a, a + n);
	for (int i = 0, j = n - 1; i < j; i++)
	{
    
    
		while (i<j&&a[i] + a[j]>m) j--;
		if (i < j&&a[i] + a[j] == m)
		{
    
    
			//记录答案
		}
	}

Since the ipointer or jpointers can be moved in one direction, i++or j--perform the most ntimes, the time complexity is O(n).

Complete code

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int main()
{
    
    
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) cin >> a[i];
	int v1, v2, flag = 0;
	sort(a, a + n);
	for (int i = 0, j = n - 1; i < j; i++)
	{
    
    
		while (i<j&&a[i] + a[j]>m) j--;
		if (i < j&&a[i] + a[j] == m)
		{
    
    
			v1 = a[i];
			v2 = a[j];
			flag = 1;
			break;
		}
	}
	if (flag) cout << v1 << ' ' << v2 << endl;
	else cout << "No Solution" << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45629285/article/details/112802387