PAT1 1048 Find Coins

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/LSC_333/article/details/91129252

Topic Link to
my github

Subject to the effect

give N N coins, and asked whether this N N coins in only two Couchu M M

Entry

Each test case comprising a
first line of two positive numbers N 1 0 5 N\leq10^5 represents the total number of coins, M 1 0 3 M\leq10^3 represents the number you want to join in the
second row N N positive numbers, represents N N values of coins (500)

Export

For each sample, the output can Couchu M M , and the difference between the two coins of the largest of the group, and small coins ahead of a large coin. If you can not Couchu outputNo Solution

Sample input

8 15
1 2 8 7 2 4 11 15

7 14
1 8 7 2 4 11 15

Sample Output

4 11

No Solution

Resolve

Because the scope of the topic, said the value of the coin, so a direct open a big array represents the number of coins of each value of the coin sorting each query.
But this question is very pit that is to say the value of the coin in question does not exceed 500, if the array will be open only to 500 返回非0errors, read other people's solution know, this coin's value is more than 500, the specific number is not I know, but 1000 will be open to AC

# -*- coding: utf-8 -*- 
# @Time : 2019/6/7 17:22 
# @Author : ValarMorghulis 
# @File : 1048.py
def solve():
    n, m = map(int, input().split())
    money = [0 for i in range(1000)]
    a = list(map(int, input().split()))
    a = sorted(a, key=lambda x: x)
    for i in range(n):
        money[a[i]] += 1
    flag = False
    for i in range(n):
        if a[i] != m - a[i]:
            if money[m - a[i]]:
                print("%d %d" % (min(a[i], m - a[i]), max(a[i], m - a[i])))
                flag = True
                break
        elif money[a[i]] >= 2:
            print("%d %d" % (a[i], a[i]))
            flag = True
            break
    if not flag:
        print("No Solution")


if __name__ == "__main__":
    solve()

Guess you like

Origin blog.csdn.net/LSC_333/article/details/91129252