Description
Christmas is approaching. Jimmy has bought many gifts for his friends. He wants to give his 1st friends a gift at the price of S1, A gift for S2 is offered to 2nd friends ..... and so on, he wants to give his Si-priced gift to his friend I. But he bought too many gifts, so that he forgot whether there was a gift at the price of Si. Fortunately, Jimmy left the shopping list.
Tell you the price of the N-piece gift purchased by Jimmy and the price of the M-piece gift he wants to give, he wants to know if he can pick out the m from the N gifts he bought for his friends. If yes, tell him "yes"; otherwise, tell him "no ".
Input
The input contains multiple groups of data.
For each group of data, the first act is two positive integers n and M (0 <n, m <= 100000), respectively, the number of purchased gifts and the number of gifts to be given. The second row contains n positive integers, which are the price of the N gifts you bought. The M positive integer in the third row. The I number represents the price of the gift you want to give to your friend. (The price is within 231)
When N = m = 0, the input ends.
Output
Each group of data outputs a row. If yes, "yes" is output; otherwise, "no" is output ".
Solution: first, we need to sort the prices of gifts and the prices of gifts given to our friends. Then, we need to search for the two arrays from the beginning. If the prices of gifts are lower than those of gifts, we will continue to look for them, until the gift price is found, if it still cannot be met, it indicates that the task cannot be implemented.
#include <iostream>#include <algorithm>using namespace std;int a[100002],b[100002];class compare{public:bool operator()(const int &x,const int &y){return x<y;};};int main(){int n,m;int i,j;compare cmp;cin>>n>>m;while(!(n==0&&m==0)){for(i=0;i<n;i++)cin>>a[i];sort(a,a+n,cmp);for( j=0;j<m;j++)cin>>b[j];sort(b,b+m,cmp);for (j=i=0; j<m; j++,i++){while (i<n && a[i]<b[j]) i++;if (i>=n || a[i]>b[j]) break;}if (j>=m) cout << "YES" << endl;else cout << "NO" << endl;cin>>n>>m;} return 0;}