Portal
A set of n 1-dimensional items has A to is packed in identical bins. All bins had exactly the same length l and each item I had length li ≤ l. We look for a minimal number of bins q such that
Each bin contains at most 2 items,
Each item is packed in one of the Q Bins,
the sum of the lengths of the items packed in a bin does not exceed l.
You are requested, given the integer values n, l, L1, ..., ln, to compute t He optimal number of bins Q.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This was followed by a blank line, and there was also a blank line between the consecutive inputs. The first line of the input file contains the number of items n (1≤ n ≤10^5). The second line contains one integer this corresponds to the bin length l ≤10000. We then had n lines containing one integer value that represents the length of the items.
Output
For each test case, your program have to write the minimal number of bins required to pack all items.
The outputs of the consecutive cases would be separated to a blank line.
Note: The sample instance and an optimal solution are shown in the figure below. Items is numbered from 1 to ten according to the input order.
Sample Input
1
10
80
70
15
30
35
10
80
20
35
10
30
Sample Output
6
------------------------------------------------
First notice the output format, UVA's question often requires the outputs of the consecutive cases would be separated by a blank line. Giant pits.
Solution:
greedy, my solution is from small to large enumeration length, each length with another as large as possible length together, if not found in the length of the individual installed.
Proof:
Contradiction Suppose there is a better scheme, one with two items i, J (set length (I ) The shorter item in container b of <=length (J) is not the longest item that can be combined with it K Together, change the position of K and J , get the same excellent plan. So the above greedy strategy leads to the optimal scheme.
Implementation:
The implementation is modeled with a map, too low.
#include <bits/stdc++.h>using namespaceStd;map<int,int>CNT;intMain () {intT scanf"%d", &T); for(intN, L, cs=0; t--;){ if(cs++) puts ("" ); scanf ("%d%d", &n, &M); for(intLen n--;) scanf ("%d", &len), cnt[len]++; intans=0; for(intNow;Cnt.empty ();) { now=cnt.begin ()First ; Ans++; Cnt.begin ()->second--; if(!cnt.begin ()second) {cnt.erase (now); } if(Cnt.empty ()) Break; Auto It=cnt.lower_bound (lNow ); if(It==cnt.end ()) it--; for(;; it--){ if(it->first<=l-Now ) {It->second--; if(!it->second) {Cnt.erase (It-First ); } Break; } if(It==cnt.begin ()) Break; }} printf ("%d\n", ans); }}
P.S. I wanted to simulate it with multiset, but later found Multiset does not support a single deletion of the same element .
multiset<int> s; int Main () { s.insert (); S.insert (ten); printf ("%d\n", S.size ()); // 2 S.erase (ten); printf ("%d\n", S.size ()); // 0}
UVA 1149 Bin Packing