贪心算法(各种贪心题目)

Posted caijiaming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了贪心算法(各种贪心题目)相关的知识,希望对你有一定的参考价值。

感觉很多贪心的题目只要想到怎么贪心就很快能解决,但是没有想到的话代码量就会很大,而且很容易出错,所有贪心还是要多做题目,掌握各种贪心的题目

题目链接:https://vjudge.net/contest/231313#problem/D

A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 6*6. Because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of parcels necessary to deliver the given products according to an order would save a lot of money. You are asked to make such a program.
Input
The input file consists of several lines specifying orders. Each line specifies one order. Orders are described by six integers separated by one space representing successively the number of packets of individual size from the smallest size 1*1 to the biggest size 6*6. The end of the input file is indicated by the line containing six zeros.
Output
The output file contains one line for each line in the input file. This line contains the minimal number of parcels into which the order from the corresponding line of the input file can be packed. There is no line in the output file corresponding to the last ``null‘‘ line of the input file.
Sample Input
0 0 4 0 0 1 
7 5 1 0 0 0 
0 0 0 0 0 0 
Sample Output
2 
1 

题目大意:有1*1,2*2···6*6尺寸的箱子,每种物品的高度确定,只用6*6的箱子,输入每种尺寸的物品有多少个,问你最少要用多少箱子,当六个0时推出循环
个人思路:6*6,5*5,4*4的物品每一个都需要一个箱子来放,所以先考虑,4个3*3物品占满一个箱子,考虑要多少个箱子,然后考虑已经用了的箱子能放多少个2*2的,不够的话再用箱子
最后考虑总共用了多少个箱子,把方块数求出来,减去用掉的方块数,就算当前的箱子能放的1*1的物品数量,不够再补箱子
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
typedef long long ll;
using namespace std;
const ll mod=1e9+7;
#define INF 0x3f3f3f
int main()
{
    ll a[10];
    ll num[4]={0,5,3,1};
    while(1)
    {
        ll ans=0;
        ll tmp=0;
        for(int i=1;i<=6;i++)
        {
            scanf("%I64d",&a[i]);
            tmp+=a[i];
        }
        if(tmp==0)
            return 0;
        ans+=a[6]+a[5]+a[4]+(a[3]+3)/4;//a6,a5,a4每个物品需要一个箱子,4个a3需要一个箱子
        ll a2=a[4]*5+num[a[3]%4];//a6,a5,a4,a3有多少个空位子可以放2*2的物品
        if(a[2]>a2)
        {
            ans+=(a[2]-a2+8)/9;
        }
        int a1=ans*36-a[6]*36-a[5]*25-a[4]*16-a[3]*9-a[2]*4;//总共用的箱子还剩多少个能放a1
        if(a[1]>a1)
        {
            ans+=(a[1]-a1+35)/36;
        }
        printf("%I64d
",ans);
    }
    return 0;
}

  





以上是关于贪心算法(各种贪心题目)的主要内容,如果未能解决你的问题,请参考以下文章

763. 划分字母区间-贪心算法

贪心算法----区间覆盖问题(POJ2376)

贪心算法+实例

算法今天的算法题目是给出两组数组拼接最大数,了解一下看看你会吗?

刷题时,遇见过哪些巧妙的贪心算法的题目?

贪心算法-第一节:贪心算法概述