pat1048. Find Coins (25)
1048. Find Coins (25)
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 15Sample Output 1:
4 11Sample Input 2:
7 14 1 8 7 2 4 11 15Sample Output 2:
No Solution
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<queue> 6 #include<vector> 7 #include<cmath> 8 #include<string> 9 #include<map> 10 #include<set> 11 using namespace std; 12 int line[100005]; 13 int main(){ 14 //freopen("D:\\INPUT.txt","r",stdin); 15 int n,m; 16 scanf("%d %d",&n,&m); 17 int i,j; 18 for(i=0;i<n;i++){ 19 scanf("%d",&line[i]); 20 } 21 sort(line,line+n); 22 23 /*for(i=0;i<n;i++){ 24 cout<<i<<" "<<line[i]<<endl; 25 }*/ 26 27 i=0; 28 j=n-1; 29 while(i<j){ 30 if(line[i]+line[j]>m){ 31 j--; 32 continue; 33 } 34 if(line[i]+line[j]<m){ 35 i++; 36 continue; 37 } 38 break; 39 } 40 if(i<j){ 41 printf("%d %d\n",line[i],line[j]); 42 } 43 else{ 44 printf("No Solution\n"); 45 } 46 return 0; 47 }