【Codeforces 1106B】Lunar New Year and Food Ordering
【链接】 我是链接,点我呀:)
【题意】
【题解】
把所有的菜按照价格升序排序. 对于每一个顾客的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);
}
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
· 程序员转型AI:行业分析