[Almost missed this question] souvenir Group

Source: Internet
Author: User

[Description]

New Year's Day is approaching. The Student Union asked Lele to be responsible for issuing souvenirs for the New Year's gala. To balance the value of souvenirs received by the participants in the party, he wants to group the purchased souvenirs according to the price, but each group can only contain two souvenirs at most, and the price of each souvenir group cannot exceed a given integer. To ensure that all souvenirs are sent in the shortest time, Lele wants the minimum number of souvenirs.
Your task is to writeProgramTo find out the one with the least number of groups in all grouping schemes, and output the least number of groups.

[Input format]

Contains n + 2 rows:
The fifth row contains an integer of W, which is the upper limit of the sum of the price of each souvenir group.
2nd act as an integer N, indicating the total number of purchased souvenirs.
3rd ~ N + 2 rows each line contains a positive integer Pi (5 <= pI <= W), indicating the price of the corresponding souvenir.

[Output format]

Only one row contains an integer, that is, the minimum number of groups.

[Example input]

100
9
90
20
20
30
50
60
70
80
90
[Sample output]

6

[Analysis]

Sort.

Left is the smallest ungrouped price, and right is the largest ungrouped price.

If a [left] + A [right] <= max, the two items are packed together and the pointer is moved.

Otherwise, separate a [right] into a group and move the pointer.

 

# Include <stdio. h> # include <stdlib. h> # define maxn 30010int A [maxn]; int n, l, R, ANS, Max; int CMP (const void * a, const void * B) {int c = * (int *) A, D = * (int *) B; return c-d;} int main () {scanf ("% d ", & MAX, & N); For (INT I = 0; I <n; ++ I) scanf ("% d", & A [I]); qsort (, n, sizeof (INT), CMP); r = n-1; while (L <= r) {if (a [l] + A [R] <= max) {+ L; -- r; ++ ans;} else {-- r; ++ ans;} printf ("% d \ n", ANS); 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.