1731 Christmas gift

Source: Internet
Author: User

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;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.