【贪心】1225 金银岛

题目描述】

某天KID利用飞行器飞到了一个金银岛上,上面有许多珍贵的金属,KID虽然更喜欢各种宝石的艺术品,可是也不拒绝这样珍贵的金属。但是他只带着一个口袋,口袋至多只能装重量为w的物品。岛上金属有s个种类, 每种金属重量不同,分别为n1,n2,...,ns同时每个种类的金属总的价值也不同,分别为v1,v2,...,vsKID想一次带走价值尽可能多的金属,问他最多能带走价值多少的金属。注意到金属是可以被任意分割的,并且金属的价值和其重量成正比。

【输入】

第1行是测试数据的组数kk,后面跟着kk组输入。

每组测试数据占3行,第1行是一个正整数w(1w10000),表示口袋承重上限。第2行是一个正整数s(1s100),表示金属种类。第3行有2s个正整数,分别为n1,v1,n2,v2,...,ns,vs分别为第一种,第二种,...,第ss种金属的总重量和总价值(1ni10000,1vi10000)

【输出】

kk行,每行输出对应一个输入。输出应精确到小数点后22位。

【输入样例】

2
50
4
10 100 50 30 7 34 87 100
10000
5
1 43 43 323 35 45 43 54 87 43

 

【输出样例】

171.93
508.00

 

贪心经典问题:

按照价值从大到小排序然后就ok了,注意多组数据要输出换行,坑了我一次

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<string>
#include<cstring>
using namespace std;
const int maxn=999999999;
const int minn=-999999999;
inline int read() {
    char c = getchar();
    int x = 0, f = 1;
    while(c < '0' || c > '9') {
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
struct node {
    int w;
    int c;
    double g;
} a[1005];
int my_comp(node ggg,node ggggg) {
    return ggg.g>ggggg.g;
}
int main() {
    int kkk,W,M;
    cin>>kkk;
    while(kkk--) {
        double cnt=0;
        cin>>W>>M;
        for(int i=1; i<=M; i++) {
            cin>>a[i].w>>a[i].c;
            a[i].g=a[i].c*1.0/a[i].w;//处理价值
        }
        sort(a+1,a+1+M,my_comp);//按价值排序
        for(int i=1; i<=M; ++i) {
            if(W>=a[i].w) {
                cnt+=a[i].c;
                W-=a[i].w;
            } else {
                cnt+=a[i].g*W;
                break;
            }
        }
        printf("%.2lf\n",cnt);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/pyyyyyy/p/10766940.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
金银岛问题是一个经典的贪心算法问题,主要思路是从当前状态出发,每次选择价值最大(或最小)的物品加入集合中,直到不能再加入物品为止。下面是使用Java实现的金银岛贪心算法示例代码: ```java import java.util.Arrays; public class GreedyAlgorithm { public static void main(String[] args) { int[] weights = {2, 3, 4, 5, 6}; int[] values = {3, 4, 5, 6, 7}; int capacity = 10; int maxValue = getMaxValue(weights, values, capacity); System.out.println("The maximum value that can be obtained is: " + maxValue); } public static int getMaxValue(int[] weights, int[] values, int capacity) { int n = weights.length; double[] ratio = new double[n]; for (int i = 0; i < n; i++) { ratio[i] = (double) values[i] / weights[i]; } int[] index = new int[n]; for (int i = 0; i < n; i++) { index[i] = i; } Arrays.sort(index, (a, b) -> Double.compare(ratio[b], ratio[a])); int maxValue = 0; for (int i = 0; i < n; i++) { int j = index[i]; if (weights[j] <= capacity) { maxValue += values[j]; capacity -= weights[j]; } else { maxValue += ratio[j] * capacity; break; } } return maxValue; } } ``` 在这个示例代码中,我们假设有5件物品,它们的重量和价值分别为 `weights` 和 `values`,我们要从中选择一些物品放入容量为 `capacity` 的背包中,使得背包中物品的总价值最大。首先,我们计算每个物品的性价比 `ratio`,并将物品的下标存储在 `index` 数组中,以便我们可以根据性价比对物品进行排序。然后,我们从性价比最高的物品开始,将它加入背包中,直到不能再加入新的物品为止。具体实现细节请参考代码注释。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值