Greedy
Sort all the items and take one from the big head to check if the minimum value is <= m at this time. If yes, take it. If no, no.
Note the output requirements for this question !!!
When there are multiple groups of samples, there must be a blank line in each of the two groups of samples!
In a group of examples, do not output extra empty rows. (Because this wa got stuck for half an hour t and finally found it by searching for other people's code on the Internet. Although some previous questions or OJ would ignore the last blank line, it is better to get rid of this bad habit)
AC code
#include <iostream>#include <cstdio> #include <algorithm>#include <cstring>#define maxn 100000+10using namespace std;int a[maxn];int cmp(int a,int b){ return a>b;}int work(int n,int m){ int ans=0; int i=0,j=n-1; while (i<=j){ if (a[i]+a[j]<=m&&i!=j) j--; i++;ans++; } return ans;}int main(){ int t=0; int testcase; scanf("%d",&testcase); while (testcase--){ t++; int n,m; scanf("%d%d",&n,&m); memset(a,0,sizeof(a)); for (int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n,cmp); if(t!=1) printf("\n"); printf("%d\n",work(n,m)); }}View code
PS. It's a bit complicated to think about your first greed. Find the biggest one, and then find the biggest one that can be stuck. I'm not sure about the correctness of this idea, but I don't have to worry about it.
Ultraviolet A 1149 bin packing