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

【题意】

给你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. POJ Area of Simple Polygons 扫描线

    这个题lba等神犇说可以不用离散化,但是我就是要用. 题干: Description There are N, <= N <= , rectangles -D xy-plane. The ...

  2. 无限极分类算法 thinkphp

    <?php/** 本类实现的是无限级递归分类的管理*/class InfiniteLevel{ public $id_str=""; public function add_ ...

  3. codevs3728联合权值(LCA)

    3728 联合权值  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 输入描述 Input Des ...

  4. BZOJ 4173 数论

    思路: $(m%k+n%k>=k) *phi(k)$ $我们不妨设n=q_1k+r_1 m=q_2k+r$2 $n+m=(q_1+q_2)k+r1+r2$ ${\lfloor}\frac{n+m ...

  5. openpyxl python操作Excel表格,

    这里openpyxl只支持xlsx格式的Excel,openpyxl使用起来会更方便一些,所以如果只操作小流水线文件的话,那么可以优先选择openpyxl,如果要兼容xls的话,就使用xlrd/xlw ...

  6. Maven+Docker,发布到Registry

    1.配置Pom.xml <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEnc ...

  7. HTML 表格与表单 个人简历

    <title>个人简历</title></head> <body background="1e30e924b899a9015b946ac41f950 ...

  8. java学习笔记_BeatBox(GUI部分)

    import java.awt.*; import javax.swing.*; public class BeatBox { JFrame theFrame; JPanel mainPanel; S ...

  9. win32之bitmap篇

    先讲一下LoadBitmap的用法,代码如下: PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd,&ps); HDC hMemDC = CreateCompa ...

  10. 如何成为一名出色的Oracle数据库管理员

    主要针对Oracle DBA在成长阶段的定位,学习方法等几大方面进行了经典的论述,详细内容请参考下文. 一.定位 Oracle分两大块,一块是开发,一块是管理.开发主要是写写存储过程.触发器什么的,还 ...