#include <bits/stdc++.h>
#define N 300010
#define PII pair<int, int>
using namespace std; typedef long long LL; int n, m, a[N], c[N], t, d;
LL ans = ; priority_queue<PII, vector<PII>, greater<PII> > Q; int main(){
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++){
scanf("%d", &a[i]);
} for (int i = ; i <= n; i++){
scanf("%d", &c[i]);
Q.push(make_pair(c[i], i));
} for (int i = ; i <= m; i++){
scanf("%d%d", &t, &d);
if (d <= a[t]){
a[t] -= d;
printf("%lld\n", 1LL * d * c[t]);
} else {
bool flag = false;
LL ans = 1LL * a[t] * c[t];
d -= a[t];
a[t] = ;
while (!Q.empty()){
while (!Q.empty() && a[Q.top().second] == ) Q.pop();
if (Q.empty()) break;
PII now = Q.top();
if (d <= a[now.second]){
a[now.second] -= d;
ans += 1LL * d * now.first;
flag = true;
printf("%lld\n", ans);
break;
} else {
ans += 1LL * a[now.second] * now.first;
d -= a[now.second];
a[now.second] = ;
Q.pop();
}
} if (!flag){
puts("");
}
}
}
}

以上是标准程序,只用了155ms,而下面的我的代码用了904ms。差距还是很大的,仔细看,发现是细节的优化。

#include<iostream>
#include<queue>
#include<algorithm>
#define pii pair<int,int>
using namespace std;
int num[];
int price[];
typedef long long LL;
priority_queue<pii,vector<pii>,greater<pii> > q;
int main(){
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++)
cin>>num[i];
for(int i=;i<=n;i++){
cin>>price[i];
q.push(make_pair(price[i],i));
}
for(int i=;i<m;i++){
int t,d;
cin>>t>>d;
LL sum=;
int remain=d;
if(num[t]>=d){
sum+=1LL*d*price[t];
num[t]-=d;
remain=;
}
else{
sum+=1LL*num[t]*price[t];
remain-=num[t];
num[t]=;
}
while(remain>){
if(q.empty()){
cout<<<<endl;
break;
}
pii f=q.top();
if(num[f.second]>remain){
sum+=1LL*remain*f.first;
num[f.second]-=remain;
remain=;
}
else{
sum+=1LL*num[f.second]*f.first;
remain-=num[f.second];
num[f.second]=;
q.pop();
}
}
if(remain==)
cout<<sum<<endl;
}
return ;
}

Codeforces Round #536 (Div. 2) B. Lunar New Year and Food Ordering的更多相关文章

  1. Codeforces Round #536 (Div. 2)--1106D - Lunar New Year and a Wander

    https://codeforces.com/contest/1106/problem/D 题意:求出字典序最小的走法 解法:走到每个点,都选取与这个点连通的序号最小的点,并且这个序号最小的点没有被访 ...

  2. Codeforces Round 536 (Div. 2) (E)

    layout: post title: Codeforces Round 536 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  3. Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂

    https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...

  4. Codeforces Round #536 (Div. 2) E dp + set

    https://codeforces.com/contest/1106/problem/E 题意 一共有k个红包,每个红包在\([s_i,t_i]\)时间可以领取,假如领取了第i个红包,那么在\(d_ ...

  5. Codeforces Round #536 (Div. 2)

    前言 如您所见这又是一篇咕了的文章,直接咕了10天 好久没打CF了 所以还是个蓝名菜鸡 机房所有人都紫名及以上了,wtcl 这次前4题这么水虽然不知道为什么花了1h,结果不知道为什么搞到一半出锅了,后 ...

  6. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  7. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  8. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  9. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

随机推荐

  1. HTML day48

    前端知识之HTML内容   HTML介绍 Web服务本质 import socket#引入套接字模块 sk = socket.socket()#实例化一个套接字对象 sk.bind(("12 ...

  2. Vue 全局注册逐渐 和 局部注册组件

    //定义一个名为 button-counter 的新组件 Script: Vue.component('button-counter',{//button-counter 这个是组件的名字 data: ...

  3. istio-jaeger-spring boot调用链配置

    istio-jaeger-spring boot调用链配置 虽然,istio ingress controller已经生成了jaeger 记录所需要的信息,但是多个分布式之间没法清晰记录相互之间的依赖 ...

  4. 5E - A + B Again

    There must be many A + B problems in our HDOJ , now a new one is coming. Give you two hexadecimal in ...

  5. hdu 1539 & poj 1416 某某公司

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1539 大意是输入n和m,把m按顺序拆分成若干个数,问这些数和的在小于n的前提下最大为多少 注意必须m的 ...

  6. 2019年Java未来的发展方向

    2018即将结束,迎来2019年,Java作为世界上 最流行的计算机编程语言,在当今信息时代中发挥了重要的作用.Java语言本身具有着自己独特的优势:面向对象.分布式应用并且安全.多线程.跨平台等.这 ...

  7. MySQL学习笔记-数据库内存

    数据库内存 InnoDB存储引擎内存由以下几个部分组成:缓冲池(buffer pool).重做日志缓冲池(redo log buffer)以及额外的内存池(additional memory pool ...

  8. python 打开文件对话框 filedialog tkinter GUI 编程

    - -读取文件的gui编程 # _*_ coding:utf-8 _*_ import tkinter from tkinter import filedialog def openfiles2(): ...

  9. vue 获取跳转上一页组件信息

    项目中有一需求,需要根据不同的页面路径url跳转进行不同的操作,首先需要获得上一页面的url,利用 beforeRouteEnter 这个钩子中的from参数获得之前url的信息,然后给 next 传 ...

  10. JDK8集合类源码解析 - HashSet

    HashSet 特点:不允许放入重复元素 查看源码,发现HashSet是基于HashMap来实现的,对HashMap做了一次“封装”. private transient HashMap<E,O ...