【链接】 我是链接,点我呀:)

【题意】

给你n个菜以及每个人需要的菜以及数量
如果某个人无法满足它对菜的需求的话
就用价格比较低的菜来填充它的要求。
(如果价格低的菜不够了,那么就直接输出0)
否则输出每个人的消费总量

【题解】

把所有的菜按照价格升序排序.
对于每一个顾客的kind,num
先减少菜的数量。
然后定义一个变量last
表示last之前的菜都已经售空(排序后,每个人可能会有多余的部分,要用前面最小的若干部分填充)
所以每个人都会让价格低的前面的一部分菜清空。
我们每次给某个顾客填充的时候,只要从那个零界点开始填充就好
然后不要一个订单一个订单(单个)的处理
而应该一个菜一个菜的遍历
这样复杂度才是线性的。

【代码】

import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner; public class Main { static class Pair implements Comparable<Pair>{
int x,id; public Pair(int x,int id) {
this.x = x;this.id = id;
} @Override
public int compareTo(Pair arg0) {
if (arg0.x==this.x)
return this.id-arg0.id;
else return this.x-arg0.x;
} } static int n,m;
static int rest[],Cost[],l[],r[];
static Pair a[]; public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
n = in.nextInt();m = in.nextInt();
rest = new int[n];
a = new Pair[n];
l = new int[n+10];
r = new int[n+10];
Cost = new int[n+10]; for(int i = 0;i < n;i++) rest[i]= in.nextInt();
for (int i = 0;i < n;i++) {
a[i] = new Pair(0,i);
a[i].x = in.nextInt();
Cost[i] = a[i].x;
}
Arrays.sort(a);
int last = 0; for (int i = 1;i <= m;i++) {
long cost = 0;
int kind,num;
kind = in.nextInt();num = in.nextInt();
kind--;
int temp = Math.min(num, rest[kind]);
rest[kind]-=temp;
num-=temp;
cost += 1l*temp*Cost[kind]; while(num>0 && last <n) {
temp = Math.min(num, rest[a[last].id]);
rest[a[last].id]-=temp;
num-=temp;
cost+=(long)1l*temp*Cost[a[last].id];
if (rest[a[last].id]==0) {
last++;
}
}
if (num!=0) cost = 0;
System.out.println(cost);
}
} }

最新文章

  1. JVM大端判断
  2. Nginx 笔记与总结(8)Location:归纳总结
  3. LINQ 按多个字段排序
  4. 由浅入深了解Thrift之微服务化应用架构
  5. [转]JSON.stringify 语法实例讲解
  6. 距离顶部估计像素,显示div!
  7. 04747_Java语言程序设计(一)_第1章_Java语言基础
  8. Arcgis for Silverlight学习(一)
  9. 【VxWorks系列】任务间同步与通信之信号量
  10. React中路由传参及接收参数的方式
  11. flutter获取状态栏高度
  12. [转]C语言的int最值问题,以及原码反码及补码
  13. javascript基础 之 void
  14. final,finally,finalize
  15. Emacs中多个golang项目的配置方法
  16. web前端名词
  17. NPOI之Excel——设置单元格背景色
  18. HTML5学习笔记(十七):访问器和class关键字
  19. 自定义 vue switch 组件
  20. cron延时

热门文章

  1. 最直观的poi的使用帮助(告诉你怎么使用poi的官网),操作word,excel,ppt
  2. 【SCOI 2011】 糖果
  3. AD9850驱动程序--MSP430版本
  4. 将本地文件复制到hadoop文件系统
  5. HIT1917Peaceful Commission(2-SAT)
  6. python值函数名的使用以及闭包,迭代器
  7. Android -----listView的重要属性
  8. JVM 垃圾回收器详解
  9. 【转】Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例
  10. Python语言之变量2(命名规则,类型转换)