原题
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
大体翻译
就是一个叫做Eva的人用两个金币付钱的故事
当没有两个刚刚好的金额付钱时输出No Solution
当一个或多个组合满足条件时以第一个金额的最小值输出
思路
第一次编写:
1 2 2 4 7 8 11 15
从头遍历每一个值,然后将其与最后一个比较,若二者和等于所需金额M,则输出结果并结束,若和大于所需金额M,则再将其与最后倒数第二个比较,若和小于所需金额M,则开始遍历第二个值,也是将其与最后一个比较,以此类推……
这样的结果是正确的,但是运行超时
第二次编写:
标记开头和结尾的位置为front和last
当front和last所指的值相加等于M时,输出结果并结束
当二者值相加大于M时,说明a[front]和a[last]前面的值相加可能满足条件,所以需要将last往前移一位
当二者值相加小于M时,说明a[front]后面的值和a[last]相加可能满足条件,所以需要将front往后移一位
两次编写比较来看,第一次编写在每一次遍历的时候都从最后开始扫描,而第二次编写每一次遍历的时候都是在前一次的基础上继续扫描,在数据较大的情况下,节省了许多时间
这也是二分查找思想在本题上的体现
第一次编写
#include <iostream>
#include <algorithm>
using namespace std;
int c[100001];
bool cmp(int a, int b)
{
return a < b;
}
int charge(int c[], int n, int m)
{
for (int i = 0; i < n; i++)
{
for (int j = n - 1; j > i; j--)
{
//cout << c[i] << " " << c[j] << endl;
if (c[i] + c[j] == m)
{
cout << c[i] << " " << c[j] << endl;
return 1;
}
else if (c[i] + c[j] < m)
{
break;
}
}
}
return 0;
}
int main()
{
int N, M;
cin >> N >> M;
for (int i = 0; i < N; i++)
cin >> c[i];
sort(c, c + N, cmp);
//for (int i = 0; i < N; i++)
// cout << c[i] << " ";
if (charge(c, N, M) == 0)
printf("No Solution\n");
return 0;
}
结果
第二次编写
#include <iostream>
#include <algorithm>
using namespace std;
int c[100010];
bool cmp(int a, int b)
{
return a < b;
}
int charge(int c[], int n, int m)
{
int front = 0;
int last = n - 1;
while (front != last)
{
//cout << "fc:" << c[front] << " " << c[last] << endl;
if (c[front] + c[last] == m)
{
cout << c[front] << " " << c[last] << endl;
return 1;
}
else if (c[front] + c[last] < m)
{
front++;
}
else if (c[front] + c[last] > m)
{
last--;
}
}
return 0;
}
int main()
{
int N, M;
cin >> N >> M;
for (int i = 0; i < N; i++)
cin >> c[i];
sort(c, c + N, cmp);
//for (int i = 0; i < N; i++)
// cout << c[i] << " ";
if (charge(c, N, M) == 0)
printf("No Solution\n");
return 0;
}