Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she could only use exactly two coins to pay the exact amount. Since she has as many as 1 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find two coins to pay for it.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (≤, the total number of coins) and M (≤, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers no more than 500. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the two face values V1 and V2 (separated by a space) such that V1+V2=M and V1≤V2. If such a solution is not unique, output the one with the smallest V1. If there is no solution, output No Solution
instead.
Sample Input 1:
8 151 2 8 7 2 4 11 15
Sample Output 1:
4 11
Sample Input 2:
7 141 8 7 2 4 11 15
Sample Output 2:
No Solution
1 #include2 #include 3 #include 4 #include 5 using namespace std; 6 int N, M; 7 //放法一,使用窗口函数 8 int main() 9 {10 cin >> N >> M;11 vector nums(N);12 vector >res;13 for (int i = 0; i < N; ++i)14 cin >> nums[i];15 sort(nums.begin(), nums.end(), [](int a, int b) { return a < b; });16 int l = 0, r = N - 1, m;17 while (l < r)//找到中间的数刚好与M/2最接近18 {19 m = (l + r) / 2;20 if (nums[m] <= M / 2 && nums[m + 1] > M / 2)21 break;22 if (nums[m] > M / 2)23 r = m - 1;24 else25 l = m + 1;26 }27 r = m + 1;28 l = m;29 while(l>=0 && r a, pair b) { return a.first < b.first; });42 if (res.size() == 0)43 cout << "No Solution" << endl;44 else45 cout << res[0].first << " " << res[0].second << endl;46 return 0;47 }48 49 //方法二,使用数找数原理50 int main()51 {52 cin >> N >> M;53 int nums[1001], t;//根据题目要新建的数组大小54 memset(nums, 0, sizeof(int) * 1001);55 for (int i = 0; i < N; ++i)56 {57 cin >> t;58 nums[t]++;//统计个数59 }60 for (int i = 0; i < 1001; ++i)61 {62 if (nums[i] > 0)//个数大于063 {64 nums[i]--;//使用掉一个数字65 if (M > i && nums[M - i] > 0)66 {67 cout << i << " " << M - i << endl;//由最小值开始遍历,故为最优答案68 return 0;69 }70 nums[i]++;//没有使用,还回去71 }72 }73 cout << "No Solution" << endl;74 return 0;75 }