算法2022 · 简单贪心

部分背包问题

//【部分背包问题】不只是取或不取
//可以取出多个,或一部分
#include <iostream>
#include <stdio.h>
using namespace std;
int n;
float v[274], w[274];
float xb[274], c;
void init()
{
    printf("部署【商品数】【背包容量】:");
    scanf("%d%f", &n, &c);
    for (int x = 1; x <= n; x++)
    {
        scanf("%f%f", &v[x], &w[x]);
        xb[x] = v[x] / w[x];
    }
    for (int x = 1; x <= n; x++)
        printf("%f ", xb[x]);
}
void change()
{
    for (int x = 1; x <= n; x++)
    {
        float t;
        for (int y = x; y <= n; y++)
        {
            if (xb[x] < xb[y])
            {
                t = xb[y];
                xb[y] = xb[x];
                xb[x] = t;
                t = v[x];
                v[x] = v[y];
                v[y] = t;
                t = w[x];
                w[x] = w[y];
                w[y] = t;
            }
        }
    }
}
void greedy()
{
    float sum = 0;
    for (int x = 1; x <= n; x++)
    {
        if (c <= 0) //背包满了
            break;
        if (c >= w[x]) //背包还能装
        {
            c -= w[x];
            sum += v[x];
            printf("c=%f  w[x]-=%f  sum=%f\n", c, w[x], sum);
        }
        else if (c > 0 && c < w[x]) //只能装下一部分
        {
            sum += v[x] * (c / w[x]);
            c = 0;
            printf("c=%f  w[x]-=%f  sum=%f\n", c, w[x], sum);
        }
    }
    printf("sum=%f\n", sum);
}
void test()
{
    printf("\n");
    for (int x = 1; x <= n; x++)
    {
        printf("value=%0.2f\tweight=%0.2f\tcost performance=%0.2f\n", v[x], w[x], xb[x]);
    }
    printf("\n");
}

int main()
{
    init();
    change();
    test();
    greedy();
    return 0;
}

/*高性价比的先装,尽量多装,转不下完整的就拆一部分下来
商品数n,背包容量c;价值v,重量w(饮料总储备)
5 800
60 600
10 250
36 200
16 100
45 300
*/

允许背包容量不足时候,取得部分的商品,而不是商品整体

贪心算法要求从【最高性价比】起取得

活动安排

// P95  活动安排问题
//启动时间,结束时间,n个活动
//选择【哪几个活动】,让活总数最大
//此处时间占用都是左闭右开
#include <iostream>
#include <stdio.h>
using namespace std;
int n, s[274], e[274];
bool pd[274];
void init()
{
    printf("活动总数:");
    scanf("%d", &n);
    for (int x = 1; x <= n; x++)
        scanf("%d%d", &s[x], &e[x]);
    for (int x = 1; x <= n; x++)
        pd[x] = false;
}
void greedy()
{
    pd[1] = true;                //第一号入选
    int t = 1;                   //前者
    for (int x = 2; x <= n; x++) //后者
    {
        if (e[t] <= s[x]) //前者结束于后者之前
        {
            t = x;
            pd[t] = true;
        }
    }
}
void ans()
{
    printf("建议举办活动:");
    for (int x = 1; x <= n; x++)
        if (pd[x])
            printf("%d ",x);
}
int main()
{
    init();
    greedy();
    ans();
    return 0;
}

/*几个活动,起始时间·终止时间
11
1 4
3 5
0 6
5 7
3 8
5 9
6 10
8 11
8 12
2 13
12 14

*/

这种贪心不是全局最优解,这里默认活动1举办前提下去寻找其他活动举办可能性

如果1很长,别人都不要办事了?

.

多机器调度问题

//多机调度问题
#include <iostream>
#include <stdio.h>
using namespace std;
int n, m, a[233], time[233] = {};
void init()
{
    printf("输入【任务总数】【机器总数】");
    scanf("%d%d", &n, &m);
    for (int x = 1; x <= n; x++)
        scanf("%d", &a[x]);
}
int gg() //【机器数】小于【任务总数】
{
    for (int x = 1; x <= n; x++)
    {
        int min = 0xfffffff, k; //记录坐标k,是机器任务中进度最快的
        for (int y = 1; y <= m; y++)
        {
            if (time[y] < min)
            {
                min = time[y];
                k = y;
            }
        }
        //一轮后,选出当前进度最快的机器
        time[k] += a[x];
    }
    int max = 1 << 31;
    for (int x = 1; x <= m; x++)
        if (max < time[x])
            max = time[x];
    return max;
}
int work()
{
    for (int x = 1; x <= n; x++)
    {
        for (int y = x; y <= n; y++)
        {
            if (a[x] < a[y])
            {
                int t;
                t = a[x];
                a[x] = a[y];
                a[y] = t;
            }
        }
    }
    if (m >= n) //待命机器数,大于等于,总任务数;一人一间即可
        return a[1];
    if (m < n)
        return gg();
}
int main()
{
    init();
    printf("max=%d", work());

    return 0;
}

/*
7 3
2 14 4 16 6 5 3
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

影月丶暮风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值