Cattle off the holiday season Team 2 A. buy one get one

link:

https://ac.nowcoder.com/acm/contest/924/A

Meaning of the questions:

Farmer John in line to buy hay. He found a sum of special deal. He bought a bunch of every size as A (1 <= A <= 1,000,000) of hay, he can get a bunch of free size B (1 <= B <A ) of hay!
However, there is a provision of the deal: a large bundle of hay must be of high quality, a small bundle of low quality. FJ is a miser, he did not care: whatever quality, as long as the money is good.
Given a set of N (1 <= N <= 10,000) high quality hay bale size, M (1 <= M < = 10,000) low quality hay bale size to find out how much to buy up to Farmer John bales of hay. He can not buy high-quality hay to get free of low quality hay, but he can not buy low-quality hay (that is, he can only be obtained by increasing sent).

Ideas:

The two bundles of grass sort, from small to large buy gifts at the same time to determine the current minimum can get, get it updated minimum.
Because of the small gifts with purchase priority to small.

Code:

#include<bits/stdc++.h>
 
using namespace std;
const int maxn = 2e5 + 10;
    int a[maxn],b[maxn];
int main()
{
    int n,m;
    cin>>n>>m;
 
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    for(int i=0;i<m;i++)
        cin>>b[i];
    sort(b,b+m);
    int cnt = 0;
    for (int i = 0;i < n;i++)
    {
        if (cnt < m && b[cnt]<a[i])
            cnt++;
    }
    cout << n+cnt << endl;
 
    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11037608.html