AcWing 1532 to find coins

Title description:
 

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 NN and MM, which represent the number of coins and the amount to be paid respectively.

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

Output format

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

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

If there is no solution, then output  No Solution.

data range

1≤N≤1051≤N≤105,
1≤M≤10001≤M≤1000

Input example 1:

8 15
1 2 8 7 2 4 11 15

Output sample 1:

4 11

Input example 2:

7 14
1 8 7 2 4 11 15

Output sample 2:

No Solution
#include <iostream>
#include <cstdio>

using namespace std;
const int MAX = 100009;

int a[MAX];
int exist[1009];
int n, m;

int main()
{
    scanf("%d%d", &n, &m);

    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        exist[a[i]] ++;
    }


    int x = -1, y = 1009;

    for(int i = 0; i < n; i++)
    {
        if(a[i] <= m)
        {
            if(exist[m - a[i]])
            {
                if(a[i] != m - a[i] || (a[i] == m - a[i] && exist[a[i]] > 1))
                {
                    x = min(a[i], m - a[i]);
                    y = min(x, y);
                }

            }
        }
    }
    if(x == -1)
        printf("No Solution\n");
    else
        printf("%d %d\n", y, m - y);
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44620183/article/details/113344888