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 15
Sample Output 1:
4 11
Sample Input 2:
7 14
1 8 7 2 4 11 15
Sample Output 2:
No Solution
超时解法,直接遍历查找:
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;
int main(){
int n,m;
scanf("%d %d",&n,&m);
int a[100001];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
int flag=0;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]+a[j]==m)
{
printf("%d %d",a[i],a[j]);
flag=1;
break;
}
}
if(flag==1)
break;
}
if(flag==0)
{
printf("No solution");
}
}
二分:
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;//将输入的数据存储在长度为N的数组A中,将A数组元素从小到大进行排序,遍历整个数组,对于遍历到的元素A[i],在[i+1,N)中利用二分查找算法查找是否有M-A[i]这个数,有则按要求进行输出;如果数组遍历完仍然没有找到,则输出"No Solution"
int main(){
int n,m;
scanf("%d %d",&n,&m);
int a[100001];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
for(int i=0;i<n;i++)
{
if(binary_search(a+i+1,a+n,m-a[i]))
{
printf("%d %d",a[i],m-a[i]);
return 0;
}
}
printf("No Solution");
return 0;
}
1085 Perfect Sequence (25 分)
Given a sequence of positive integers and another positive integer p. The sequence is said to be a perfect sequence if M≤m×p where M and m are the maximum and minimum numbers in the sequence, respectively.
Now given a sequence and a parameter p, you are supposed to find from the sequence as many numbers as possible to form a perfect subsequence.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N (≤105) is the number of integers in the sequence, and p (≤109) is the parameter. In the second line there are N positive integers, each is no greater than 109.
Output Specification:
For each test case, print in one line the maximum number of integers that can be chosen to form a perfect subsequence.
Sample Input:
10 8
2 3 20 4 5 1 6 7 8 9
Sample Output:
8
二分查找
算法设计
先将读取得到的数组A从小到大进行排序,然后遍历数组A,遍历过程中,假设正在访问的元素索引为i,数组长度为N,那么可以在[i+1,N)的范围索引中查找第一个使得A[j]>A[i]*p的索引 j。那么j-i即为以A[i]为最小值的完美数列的最长长度。遍历过程中逐步更新这一最长长度,即可得到最终结果。在C++中以二分查找为基础的实现在一个容器中查找第一个大于某个值的函数为upper_bound(begin,end,val,comp)函数,它有四个参数,第一个参数表示查找范围的左界指针,第二个参数表示查找范围的右界指针,第三个参数表示要进行比较的目标值,第四个参数表示比较函数,这个函数返回一个指向第一个大于val的元素的指针。
注意点
由于给出的数以及p均<=10的9次方,故A[i]*p可能超出int的存储范围,故可利用long long储存
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;
int main(){
long long n,p;
scanf("%lld %lld",&n,&p);
long long a[100001];
for(int i=0;i<n;i++)
{
scanf("%lld",&a[i]);
}
sort(a,a+n);
int max1=0;
for(int i=0;i<n;i++)
{
int t=upper_bound(a+i+1,a+n,a[i]*p)-a-i;
max1=max(max1,t);
}
printf("%d",max1);
}
题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。