1149,1149 train
The greedy algorithm can be used to sort the length of all items, and then check whether the shortest and longest items can be put into a bag. If not, the current largest item should be put in a separate bag. If possible, put the two items in a bag, and then I ++; j --; repeat the above operation.
# Include <bits/stdc ++. h> using namespace std; const int maxn = 100000 + 5; int T, n, l, a [maxn]; int main () {scanf ("% d ", & T); while (T --) {scanf ("% d", & n, & l); for (int I = 0; I <n; I ++) scanf ("% d", & a [I]); sort (a, a + n); int ans = 0; for (int I = 0, j = n-1; I <= j;) {if (a [I] + a [j]> l) {j --; ans ++;} else {j --; I ++; ans ++ ;}} printf ("% d \ n", ans); if (T) printf ("\ n") ;}return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.