题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1048
查询
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
vector<int>coin;
int n, m;
int FindBest(int i)
{
int l=i+1, r=coin.size()-1;
while(l <= r)//如果要精确的查找到某个值,此时用<=.而1044中的用法是结合情况优化的
{
int mid =(l+r)/2;
if(coin[mid]+coin[i] == m)
{
return mid;
}
else if(coin[mid]+coin[i] > m)
{
r = mid-1;
}
else
{
l = mid+1;
}
}
return -1;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("E:\\in.txt", "r", stdin);
#endif
int t;
scanf("%d%d", &n, &m);
while(n-->0)
{
scanf("%d", &t);
coin.push_back(t);
}
sort(coin.begin(), coin.end());
int i;
for(i=0; i<coin.size(); i++)
{
if(coin[i] >= m)
{
printf("No Solution\n");
break;
}
int j=FindBest(i);
if(j == -1)
{
continue;
}
else
{
printf("%d %d\n", coin[i], coin[j]);
return 0;
}
}
if(i == coin.size())
{
printf("No Solution\n");
}
return 0;
}