【USACO】Mixing Milk 混合牛奶
2016-04-28 15:55
239 查看
题目省略
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class J { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); // 牛奶制造者的一天需要牛奶的数量 int N = sc.nextInt(); // 他们可能从农民那买到的数目 int M = sc.nextInt(); // 定义二维数组接收牛奶价格及牛奶数量 int[][] a = new int[M][2]; for (int i = 0; i < a.length; i++) { a[i][0] = sc.nextInt(); a[i][1] = sc.nextInt(); } // 对二维数组进行排序 Arrays.sort(a, new Comparator<int[]>() { // 方法重写,对数组中的牛奶价格进行排序 @Override public int compare(int[] arg0, int[] arg1) { // TODO Auto-generated method stub return arg0[0] - arg1[0]; } }); // 循环的下标 int i = 0; // 所需最小金额 int Min = 0; // 从最小的开始计算,当到达所需的牛奶数量时停止 while (N > 0) { if (N - a[i][1] >= 0) { N -= a[i][1]; Min += a[i][0] * a[i][1]; } else { Min += N * a[i][0]; N = 0; break; } i++; } System.out.println(Min); } }
相关文章推荐
- JSON
- Android Volley完全解析(一),初识Volley的基本用法
- 配置VirtualBox共享文件夹
- 自定义RadioGrop,支持添加包裹着的RadioButton
- JavaPersistenceWithMyBatis3笔记-第5章Configuring MyBatis in a Spring applications-001
- 深入理解Java之线程池(下)
- Error:Execution failed for task ':app:transformClassesWithDexForUmengDebug'.
- 值得推荐的android开源框架
- 编译LAPACK 3.6.0
- 深入理解Java之线程池(中)
- bzoj 3295: [Cqoi2011]动态逆序对(树套树)
- 10分钟让你有胆量在众人前演讲(1)
- 341. Flatten Nested List Iterator
- 如何让Sublime2中支持Markdown
- Xcode7最新app打包发布详细过程(一)
- 字符串和数组翻转
- oracle 11g审计关闭,及删除日志
- .NET中的LINQ(摘抄)
- 资源网站
- nyoj 聪明的小明--49