Nyoj-71-canoe trip

Source: Internet
Author: User
Trip on a canoe Time limit:3000MS | memory limit:65535KB Difficulty: 2
Description

During a canoe trip, the canoe can be rented at the port and there is no difference between them. A canoe can only take two people at most, and the total weight of a passenger cannot exceed the maximum carrying capacity of the canoe. We need to minimize the cost of this event, so we need to find the minimum number of canoes that can accommodate all passengers. WriteProgramThe maximum carrying capacity, number of passengers, and weight of each passenger. Based on the given rules, calculate the minimum number of canoes required for the placement of all passengers and output the results.

Input
Input s in the first line, indicating the number of test data groups;
The first row of each group of data contains two integers: W, N, 80 <= W <= 300, 1 <= n <=, W indicates the maximum carrying capacity of a canoe, and N indicates the number of people;
The next set of data is the weight of each person (not greater than the carrying capacity of the ship );
Output
The minimum number of canoes required for each group.
Sample Input
385 65 84 85 80 84 8390 390 45 60100 550 50 90 40 60
Sample output
 
533

Analysis: first, we should sort by size from small to large. After sorting, we thought that if the smallest weight cannot be the same as the largest weight, then the heavyweight will have to take a boat by themselves. This question can simulate this idea and constantly screen outSit-togetherOnly one person can sit in a few pairs and remove them. That is, after sorting, let the smallest and largest addition compare with the ship weight limit. If possible, both sides will be reduced by 1; otherwise, right minus 1, and sum ++; then, and then, the answer will come out.

DetailsCodeAs follows:

 
# Include <stdio. h ># include <algorithm> using namespace STD; int main () {int t, n, m, I, sum, J, s [301]; scanf ("% d", & T); While (t --) {scanf ("% d", & M, & N); for (I = 0; I <n; I ++) scanf ("% d", & S [I]); sort (S, S + n); for (I = 0, j = n-1, sum = 0;) {If (j <I) break; If (j = I) {sum ++; break ;} if (s [I] + s [J]> m) {sum ++; j --; continue;} If (s [I] + s [J] <= m) {sum ++; I ++; j --; Continue ;}} printf ("% d \ n", sum );}}

 
 

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.