蓝桥杯Java ABC组 LG P1417 烹调方案

题目链接:
https://www.luogu.com.cn/problem/P1417

#01背包 #贪心 #Favorite

和能量石那道题一样,也和这道题 [[LG P1080 国王游戏]] 有关系

为什么能只考虑相邻的两个呢?

因为交换相邻的两个物品,只会对这两个物品产生影响,不会对这两个物品前面的值和后面的值产生影响

具体的推导过程见下图:

30DCB4A234C51E20FB896BCFD5F92301

代码

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;

public class Main {

    static int status;
    static BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    static PrintWriter cout = new PrintWriter(bw);
    static StreamTokenizer st = new StreamTokenizer(buf);

    public static int nextInt() throws IOException {
        status = st.nextToken();
        return (int) st.nval;
    }

    public static long nextLong() throws IOException {
        status = st.nextToken();
        return (long) st.nval;
    }

    public static String nextString() throws IOException {
        status = st.nextToken();
        return st.sval;
    }

    public static String nextLine() throws IOException {
        String line = buf.readLine();
        while (line.isEmpty())
            line = buf.readLine();
        return line;
    }

    static int n, m,cnt=0;
    static int[] a,b,c;
    static long[] f;
    static Pair[] p;
    static final int INF = 0x3f3f3f3f, MOD = (int) 1e9+7;

    public static void main(String[] args) throws IOException {
        m = nextInt();
        n = nextInt();
        a = new int[n+1];
        b = new int[n+1];
        c = new int[n+1];
        f = new long[m+1];
        p = new Pair[n+1];

        for(int i=1;i<=n;i++) a[i] = nextInt();
        for(int i=1;i<=n;i++) b[i] = nextInt();
        for(int i=1;i<=n;i++) c[i] = nextInt();

        for(int i=1;i<=n;i++)
            p[i] = new Pair(a[i], b[i], c[i]);
        
        Arrays.sort(p,1,1+n);

        for(int i=1;i<=n;i++)
            for(int j=m;j>=p[i].c;j--)
                f[j]=max(f[j],f[j-p[i].c]+p[i].a-(long)j*p[i].b);
       
        long ans=0;
        for(int i=1;i<=m;i++)
            ans = max(ans,f[i]);
        cout.println(ans);
        cout.flush();
    }// End of main

    static class Pair implements Comparable<Pair>{
        int a,b,c;

        public Pair(int a,int b,int c){
            this.a = a;
            this.b = b;
            this.c = c;
        }

        @Override
        public int compareTo(Pair that){
            return this.c*that.b-this.b*that.c;
        }
    }
}
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值