Codeforces Round #536 (Div. 2) B. Lunar New Year and Food Ordering
#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的更多相关文章
- Codeforces Round #536 (Div. 2)--1106D - Lunar New Year and a Wander
https://codeforces.com/contest/1106/problem/D 题意:求出字典序最小的走法 解法:走到每个点,都选取与这个点连通的序号最小的点,并且这个序号最小的点没有被访 ...
- Codeforces Round 536 (Div. 2) (E)
layout: post title: Codeforces Round 536 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- 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} ...
- Codeforces Round #536 (Div. 2) E dp + set
https://codeforces.com/contest/1106/problem/E 题意 一共有k个红包,每个红包在\([s_i,t_i]\)时间可以领取,假如领取了第i个红包,那么在\(d_ ...
- Codeforces Round #536 (Div. 2)
前言 如您所见这又是一篇咕了的文章,直接咕了10天 好久没打CF了 所以还是个蓝名菜鸡 机房所有人都紫名及以上了,wtcl 这次前4题这么水虽然不知道为什么花了1h,结果不知道为什么搞到一半出锅了,后 ...
- 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 ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
随机推荐
- python 数据类型 之 tuple 元组
python 3.6.5 元组的特性和定义 与列表类型 只不过 [ ] 改成了() 特性: 1.不可变(元组本身不可变,但是可以存可变类型的element){猜测因为可变element的地址不可变而 ...
- python消息队列Queue
实例1:消息队列Queue,不要将文件命名为"queue.py",否则会报异常"ImportError: cannot import name 'Queue'" ...
- shell判断文件后缀名是否为特定字符串
如果文件是 .css文件 或 .js文件,则进行处理. if [ "${file##*.}"x = "css"x ]||[ "${file##*.}& ...
- Laravel自定义Api接口全局异常处理
在做API时,需要对一些异常进行全局处理,比如添加用户执行失败时,需要返回错误信息 // 添加用户 www.bcty365.com $result = User::add($user); if(emp ...
- word2vec_训练模型
from gensim.models import Word2Vecfrom gensim.models.word2vec import LineSentence # 原始的训练语料转化成一个sent ...
- 商业化博客平台原型制作分享-TypePad
TypePad是商业化博客平台,提供完全付费的博客服务,通过TypePad用户可以很容易建立自己的博客,TypePad提供博客系统该有的所有功能以及多种设计风格和外观,如果用户熟悉HTML和CSS,完 ...
- java Mather 的 group 含义
参考博文: http://blog.csdn.net/java2king/article/details/4395067 明白了group 的 含义 public class Test { pub ...
- 在Tomcat中部署Spring jpetstore
第三篇:在Tomcat中部署Spring jpetstore 博客分类: Java之web SpringTomcatMySQLJDBCMVC Spring samples中的jpetstore,基于 ...
- 【Linux】CentOS 7.4 安装 MySQL 8.0.12 解压版
安装环境/工具 1.Linux(CentOS 7.4版) 2.mysql-8.0.12-el7-x86_64.tar.gz 安装步骤 参考:https://dev.mysql.com/doc/refm ...
- Element ui 使用 Tree 树形控件
使用树形控件需要映入 jsx才能运行链接:https://github.com/vuejs/babel-plugin-transform-vue-jsx#usage npm install\ babe ...