1048. Find Coins

寻找{(x,y)|x+y=m,x,y∈S}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <unordered_map>
using namespace std;

int main()
{
int n, m;
cin >> n >> m;
unordered_map<int, int> S;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
S[x]++;
}

int minx = 0;
for (auto &&k : S) {
int x = k.first;
int y = m - x;
if (x < y && S.find(y) != S.end() || x == y && S[x] > 1) {
if (!minx || x < minx)
minx = x;
}
}
if (minx) {
cout << minx << " " << m - minx << endl;
} else {
cout << "No Solution" << endl;
}
return 0;
}