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
题目大意:
伊娃喜欢收集来自世界各地的硬币,包括一些其他的行星,比如火星。有一天她去了一个环球购物中心,那里可以用各种支付方式进行支付。但是,有一个特别的要求:每个帐单只能用两个硬币来支付。因为她有多达10的5次方个硬币,他的确需要你的帮助。你应该告诉她,无论多少钱,他是否可以找到两个硬币来支付。
输入规格:
每个输入文件包含一个测试用例。对于每种情况,第一行包含两个正整数N(<=10的5次方,代表硬币的总数)和M(<=10的3次方,伊娃必须支付的金额)。第二行包含了N个面值的硬币,他们都是面值不超过500的硬币。直线上的所有数字用空格给隔开。
输出规范:
对于每个测试用例,在一行中打印两张面值为V1和V2(有空格隔开)的硬币,这样V1+V2=M和V1<=V2。如果这样的就过不是唯一的,那么输出V1最小的那个结果。如果没有解决方案,则输出“No Solution”。
代码:
#include<stdio.h>
#include<stdlib.h>
int arr[100001];
int cmp(const void *a,const void *b)
{
return *(int *)a>*(int *)b?1:-1;
}
int main()
{
int i,j,n,m,k,t;
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
qsort(arr,n,sizeof(arr[0]),cmp);
for(i=0,j=n-1;i<j;)
{
if(arr[i]+arr[j]>m)
{
j--;
}
else if(arr[i]+arr[j]<m)
{
i++;
}
else
{
printf("%d %d\n",arr[i],arr[j]);
break;
}
}
if(i>=j)
printf("No Solution\n");
return 0;
}