蓝桥算法训练__普及组.Day10

第一题:P1359 租用游艇 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

package 蓝桥算法训练__普及组.Day10;

import java.io.*;

/**
 * @author snippet
 * @data 2023-02-14
 * P1359 租用游艇 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
 */
// 动态规划
public class T1 {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(br);
    static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

    static int n;// n表示出租站的个数
    static int[][] a = new int[210][210];// 存出租站i->j的租金
    static int[] dp = new int[210];// 表示出租站i到n的最小租金

    public static void main(String[] args) throws IOException {
        n = nextInt();
        for (int i = 1; i < n; i++) {
            for (int j = i+1; j <= n; j++) {
                a[i][j] = nextInt();
            }
            dp[i] = (int)1e6;
        }

        // 求1->n出租站的最小租金
        // 1->n = min(1->n, (1->j)+(j->n))
        // dp[i] = min(dp[i], dp[j]+a[i][j])
        for (int i = n-1; i >= 1; i--) {
            for (int j = i+1; j <= n; j++) {
                dp[i] = Math.min(dp[i], dp[j]+a[i][j]);
            }
        }
        pw.println(dp[1]);
        pw.flush();
    }

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

第二题:P1060 [NOIP2006 普及组] 开心的金明 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

package 蓝桥算法训练__普及组.Day10;

import java.io.*;

/**
 * @author snippet
 * @data 2023-02-14
 * P1060 [NOIP2006 普及组] 开心的金明 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
 */
// 01背包
public class T2 {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(br);
    static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

    static int n,m;// n存妈妈给的钱 m存金明可以选择的物品的个数
    static int[] val = new int[30];// 存每个物品的价格
    static int[] w = new int[30];// 存每个物品的权重(价格*重要程度)
    static int[] dp = new int[30300];// 存购买i价格的东西的最大权重

    public static void main(String[] args) throws IOException {
        n = nextInt();
        m = nextInt();
        for (int i = 1; i <= m; i++) {
            val[i] = nextInt();
            w[i] = nextInt() * val[i];
        }

        // 每个物品只可以放一次
        for (int i = 1; i <= m; i++) {
            for (int j = n; j >= val[i]; j--) {
                dp[j] = Math.max(dp[j], dp[j-val[i]]+w[i]);
            }
        }
        pw.println(dp[n]);
        pw.flush();
    }

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

第三题:P1802 5 倍经验日 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

package 蓝桥算法训练__普及组.Day10;

import java.io.*;

/**
 * @author snippet
 * @data 2023-02-14
 * P1802 5 倍经验日 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
 */
// 动态规划
public class T3 {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StreamTokenizer st = new StreamTokenizer(br);
    static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));

    static int n,m;
    static int[] dp = new int[1010];// 使用i瓶药水可以获得的最大经验
    static int[] lose = new int[1010];// 存失败的经验
    static int[] win = new int[1010];// 存成功的经验
    static int[] use = new int[1010];// 存打败这个玩家需要使用的药水的数量

    public static void main(String[] args) throws IOException {
        n = nextInt();
        m = nextInt();
        for (int i = 1; i <= n; i++) {
            lose[i] = nextInt();
            win[i] = nextInt();
            use[i] = nextInt();
        }

        for (int i = 1; i <= n; i++) {
            // max(打得过,打不过)
            for (int j = m; j >= use[i]; j--) {
                dp[j] = Math.max(dp[j]+lose[i], dp[j-use[i]]+win[i]);
            }
            // 绝对打不过
            for (int k = use[i]-1; k >= 0; k--) {
                dp[k] += lose[i];
            }
        }
        pw.println((long)5*dp[m]);
        pw.flush();
    }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值