Ultraviolet A 1149 bin packing

Source: Internet
Author: User

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

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.