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

【题意】

给你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);
}
} }

【Codeforces 1106B】Lunar New Year and Food Ordering的更多相关文章

  1. 【Codeforces 1106E】Lunar New Year and Red Envelopes

    [链接] 我是链接,点我呀:) [题意] 给你k个红包,每个红包可以在si..ti的时间范围内拿走. 抢完红包之后你得到wi元,然后你需要在di+1时刻才能继续抢红包 时间是线性的从1..n 然后某个 ...

  2. 【Codeforces 1106D】Lunar New Year and a Wander

    [链接] 我是链接,点我呀:) [题意] 让你遍历n个节点,访问过的节点不操作. 如果是没有访问过的点,那就把它加到序列的末尾. 问你形成的最小字典序的序列是多少. [题解] 显然每次找最小的标号 用 ...

  3. 【Codeforces 1106C】Lunar New Year and Number Division

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 看了下样例解释就懂了... 每次选择最大最小的两个组合 然后加起来.. [代码] import java.io.IOException; im ...

  4. 【Codeforces 1106E】 Lunar New Year and Red Envelopes

    Codeforces 1106 E 题意:有\(k\)个红包,第\(i\)个红包可以在\(s_i\)到\(t_i\)的时间内抢,同时获得\(w_i\)的钱,但是抢完以后一直到\(d_i\)都不可以继续 ...

  5. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  6. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  7. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  8. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  9. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

随机推荐

  1. bzoj 4590: [Shoi2015]自动刷题机

    好恶心.. 二分上界到100000LL*1000000000LL  %_% #include<cstdio> #include<iostream> #include<cs ...

  2. 华为FusionSphere概述——计算资源、存储资源、网络资源的虚拟化,同时对这些虚拟资源进行集中调度和管理

    华为FusionSphere概述 FusionSphere是华为自主知识产权的云操作系统,集虚拟化平台和云管理特性于一身,让云计算平台建设和使用更加简捷,专门满足企业和运营商客户云计算的需求.华为云操 ...

  3. 乐视云监控数据存放到influxdb中

    3.9     监控.告警系统 监控报警我们分PaaS平台和业务应用两大类. PaaS平台主要聚焦在基础设施和LeEngine的各个服务组件的监控报警(比如主机CPU,内存,IO,磁盘空间,LeEng ...

  4. mybatis传参问题总结

    一. 传入单个参数 当传入的是单个参数时,方法中的参数名和sql语句中参数名一致即可 List<User> getUser(int id); <select id="get ...

  5. mybatis多个参数查询问题

    一.话不多数,错误如下 Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException ...

  6. 记一次MySQL索引优化

    两张表是主(CHECK_DRAWINGS)从(CHECK_DRAWINGS_IMG)关系. CHECK_DRAWINGS,主表数据 3591条. SELECT COUNT(*) FROM CHECK_ ...

  7. 数据库得到too many connections”错误信息

    查进程  show processlist删除进程  kill ID查完整sql  show full processlist; 连数据库   MySQL -S /tmp/mysql.sock   或 ...

  8. 【转】Linux下history命令用法

    转自:http://blog.sina.com.cn/s/blog_5caa94a00100gyls.html 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你 ...

  9. python框架之Flask基础篇(四)-------- 其他操作

    1.蓝图 要用蓝图管理项目,需要导入的包是:from flask import Buleprint 具体大致分为三步: 1.先在子模块中导入蓝图包,然后再创建蓝图对象. 2.然后将子模块中的视图函数存 ...

  10. build.gradle(Mdule.app)依赖库相关

    dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) //noinspection GradleCompati ...