Ben Ben Bear moving packing items

Source: Internet
Author: User

Ben Ben Bear moving packing items
Narrative Description:
Stupid bear in the forest can also be happy today, friend-he bought a new house, to move to the new hi. So Please take a lot of stupid friends to help move, and ready to very need to move the small pallet of goods, as long as these cartons are the same volume, and can accommodate up to only two articles. But, friends, stupid bear think of a "smart" approach: make each carton use the most efficient (note: Only the carton volume is larger than the volume of the goods and feel can be installed. The volume of the item will not be larger than the carton volume, so the tray needs to be minimal.

In order to help dumb bear to inform friends in advance. Please follow the method of the dumb bear. Help figure out: What is the minimum number of cartons required?


Execution time limit: Unlimited
Memory Limit: Unlimited
Input:
Integer v--The volume of the paper tray.
Integer n--The total number of items n;
A total of n integers (the volume of the corresponding n items, each integer separated by a space).

Output:
Integer m--The minimum number of cartons required;

Example input:
10
2
2 3
Example output:
1

My idea is that the volume of the input object is now sorted from large to small (also small to large), then the largest and smallest volume and not more than the volume of the two are placed in a box, or the largest one in a box alone.

Assume that the last remaining one is placed separately. Add 1 boxes each time you place the box.

#include <iostream> #include <vector> #include <algorithm> using namespace std;bool order (int a,int b);   int main () {int v,n,m=0,input,first,last;vector<int> obj;    cout << "Carton volume V:"; Cin >> v;cout << "Total number of items N:"; cin >> N;cout << "The volume of these n items is each:"; for (int i=0;i!=n;i++) {cin >> input;obj.push_back (input);} cout << "entered is:"////for (i=0;i!=n;i++)//{////cout <<obj[i]<< "";//}//cout <<endl;sort (obj  . Begin (), Obj.end (), order); Arranges the elements in obj in descending order. cout << "after sorting:";//for (int so=0;so!=obj.size (); so++) cout << obj[so]<< "";//cout <&lt ; Endl;                                     first=0; Larger items last=obj.size ()-1; Smaller items if (obj[first]>v) {cout << "ERROR: Number of inputs exceeds container volume" <<endl; return 1;}                               if (n==1) m=1; There is only one item. while (First<last) {if (obj[first]+obj[last]<v+1)//large items are placed with smaller items. {m++;first++;last--;}                else {m++;first++;}     Larger items are placed separately.                    if (first==last) m++; Last remaining a separate}cout << "at least" << m<< "containers" << Endl;return 0;} BOOL Order (int A,int b) {return a>b;//descending order}


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Ben Ben Bear moving packing items

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.