Souvenir grouping

1 Problem description

The New Year's Day is approaching, and the school students will let Lele be responsible for the distribution of souvenirs for the New Year's party. In order to make the value of the souvenirs obtained by the students attending the party relatively balanced, he will group the purchased souvenirs according to the price, but each group can only include at most two souvenirs, and the sum of the prices of each group of souvenirs cannot exceed a given value. the integer. In order to ensure that all souvenirs are distributed in the shortest possible time, Lele hopes to have the least number of groups.

Your task is to write a program that finds the one with the least number of groups among all the grouping schemes and outputs the smallest number of groups.

input format

input contains  n+2n + 2  lines.

1st  _1  line contains an integer w (80 \leq w \leq 200)w ( 8 0 w 2 0 0 ) is the upper limit of the sum of the prices of each group of souvenirs.

2nd  _Line 2  is an integer n(1\leq n \leq 3\times 10^4)n(1n3×104 ), indicating the total number of souvenirs purchased.

3rd  _3  to n+2n + 2  lines each containing a positive integer P_i (5 \leq P_i \leq w)Pi(5Piw ) represents the price of the corresponding souvenir.

output format

The answer is output on one line, and the minimum number of groups is output.

sample input

Sample output



2 Reference code (C++)

#include<bits/stdc++.h>
using namespace std;
int a[30005];
intmain()
{
    int i;
    int upperLimit,n,ans,l,r;
    cin>>upperLimit>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    ans=n;l=0;r=n-1;
    while(l<r)
    {
        if(a[l]+a[r]<=upperLimit)
        {
            years--;
            l++;
            r--;
        }
        else
            r--;
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324853409&siteId=291194637
Recommended