#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. Jenkins与SVN持续集成

    官网下载Jenkins&SVN&eclipse,版本号没要求,建议使用最新稳定版本 登录Jenkins:http://localhost:8080 登录SVN:http://local ...

  2. iOS.Location-Based Service

    基于位置区域的服务 1. 背景 Ref[1] 在iOS设备锁屏的状态下,App的icon会出现在屏幕的左下角. iOS 8 Feature: Location-based Lockscreen App ...

  3. JS部分

    前端三剑客(HTML,CSS,JavaScript) Html:负责一个页面的结构 Css:负责一个页面的样式 JavaScript:负责与用户进行交互 JS概念 JS是JavaScript的简称,是 ...

  4. 文档根元素 "mapper" 必须匹配 DOCTYPE 根 "configuration"

    该问题是因为xml的头部写错了,一个是configuration,一个是mapper,不能直接复制. 参考链接:http://blog.csdn.net/testcs_dn/article/detai ...

  5. HDU 5988.Coding Contest 最小费用最大流

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  6. MySQL学习笔记-大纲

    软件程序性能测试在之前<品味性能之道>系列中已经大量提到,讲解了很多测试方法.测试观念.测试思想等等.最近准备深入MySQL进行学习并总结.分别查阅<MySQL性能调优与架构设计&g ...

  7. 事务 TRANSACTION

    事务是数据库中一个但单独的执行单元(Unit),他通常由高级数据库操作语言(如SQL)或编程语言(如C++.Java)编写的用户程序的执行所引起.当在数据库中更改数据成功时,在事务中更改的数据便会提交 ...

  8. PropertyPlaceholderConfigurer

    PropertyPlaceholderConfigurer Spirng在生命周期里关于Bean的处理大概可以分为下面几步: 加载 Bean 定义(从xml或者从@Import等) 处理 BeanFa ...

  9. rbtposeekf的注意事项

    1.发布的odom topic以及 imu topic必须加上协方差部分:2.在发布odom的时候,去掉里面的odom->base_link的tf,因为这个tf会在robot_pose_ekf包 ...

  10. 关于js的function.来自百度知道的回答,学习了.

    在js中,创建一个函数对象的语法是var myFunction = new Function(arg1,…,agrN, body);其中,该函数对象的N个参数放在 函数主体参数body的前面,即函数主 ...