蓝桥杯第六天:答疑

原题:
在这里插入图片描述
在这里插入图片描述
假设三位学生为a(a1,a2,a3),b(b1,b2,b3),c(c1,c2,c3),由样例说明可知:
a用时:a1+a2=10000+10000=20000
b用时:a1+a2+a3+(b1+b2)=10000+10000+10000+(30000+20000)=80000
c用时:a1+a2+a3+(b1+b2+b3)+(c1+c2)=10000+10000+10000+30000+20000+30000+20000+50000=180000
因此最小之和是20000+80000+180000=280000
思路了解,下面便是代码实现:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Student> list = new ArrayList<Student>();
        // 接收学生所用的时间s、a、e
        for(int i = 0; i < n; i++) {
            list.add(new Student(sc.nextInt(),sc.nextInt(),sc.nextInt()));
        }
       	// 用比较器排序,按s + a用时最少到最多排列
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.sum + o1.e > o2.sum + o2.e) {
                    return 1;
                } else if(o1.sum + o1.e < o2.sum + o2.e) {
                    return -1;
                }
                return 0;
            }
        });
        long ans = 0;
        long time = 0;
        for(int i = 0; i < n; i++) {
        	// 这里是答题用的时间sum由Student构造方法可知sum = s + a
            ans += time += list.get(i).sum;
            // 这里sum = sum + e
            time += list.get(i).e;
        }
        System.out.println(ans);
        sc.close();
    }

    static class Student {
        int s;
        int a;
        int e;
        int sum;
        public Student(int s, int a, int e) {
            this.s = s;
            this.a = a;
            this.e = e;
            this.sum = this.s + this.a;
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值