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 105 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 (≤105, the total number of coins) and M (≤103, 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 15
1 2 8 7 2 4 11 15
Sample Output 1:
4 11
Sample Input 2:
7 14
1 8 7 2 4 11 15
Sample Output 2:
No Solution
分析:散列水题
1 /**
2 * Copyright(c)
3 * All rights reserved.
4 * Author : Mered1th
5 * Date : 2019-02-25-21.22.03
6 * Description : A1048
7 */
8 #include<cstdio>
9 #include<cstring>
10 #include<iostream>
11 #include<cmath>
12 #include<algorithm>
13 #include<string>
14 #include<unordered_set>
15 #include<map>
16 #include<vector>
17 #include<set>
18 using namespace std;
19 const int maxn=100010;
20 int hashtable[maxn]={0};
21 int main(){
22 #ifdef ONLINE_JUDGE
23 #else
24 freopen("1.txt", "r", stdin);
25 #endif
26 int n,m,t;
27 cin>>n>>m;
28 for(int i=0;i<n;i++){
29 scanf("%d",&t);
30 hashtable[t]++;
31 }
32 int i;
33 for(i=0;i<=m/2+1;i++){
34 if((hashtable[i]!=0 &&hashtable[m-i]!=0&&m-i!=i)||(m-i==i)&&hashtable[i]>=2){
35 printf("%d %d\n",i,m-i);
36 return 0;
37 }
38 }
39 if(i==m/2+2) printf("No Solution");
40 return 0;
41 }
分析:也可以用二分法求解, 也可以用双指针遍历
1 /** 2 * Copyright(c) 3 * All rights reserved. 4 * Author : Mered1th 5 * Date : 2019-02-26-17.55.18 6 * Description : A1048 7 */ 8 #include<cstdio> 9 #include<cstring> 10 #include<iostream> 11 #include<cmath> 12 #include<algorithm> 13 #include<string> 14 #include<unordered_set> 15 #include<map> 16 #include<vector> 17 #include<set> 18 using namespace std; 19 const int maxn=100010; 20 int a[maxn]; 21 int Bin(int left,int right,int key){ 22 int mid; 23 while(left<=right){ 24 mid=(left+right)/2; 25 if(a[mid]==key) return mid; 26 else if(a[mid]>key) right=mid-1; 27 else left=mid+1; 28 } 29 return -1; 30 } 31 32 33 int main(){ 34 #ifdef ONLINE_JUDGE 35 #else 36 freopen("1.txt", "r", stdin); 37 #endif 38 int n,m,i; 39 scanf("%d%d",&n,&m); 40 for(int i=0;i<n;i++){ 41 scanf("%d",&a[i]); 42 } 43 sort(a,a+n); 44 for(i=0;i<n;i++){ //枚举左边界 45 int j=Bin(0,n-1,m-a[i]); 46 if(j!=i&& j!=-1){ 47 printf("%d %d\n",a[i],a[j]); 48 break; 49 } 50 } 51 if(i==n) printf("No Solution\n"); 52 return 0; 53 }