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连试试水的深浅..... ...
随机推荐
- 对于devexpress gridview 内插图加加进度条等的一点解读
如上图,gategory 加了小图标, 其他行内还有计算器,大图片 进度条等 using System; using System.Drawing; using System.Collection ...
- Oracle_高级功能(10) 备份恢复
备份与恢复Oracle数据库有三种标准的备份方法,分别是导出/导入(EXP/IMP).热备份和冷备份.导出/导入是一种逻辑备份,冷备份和热备份是物理备份.一.导出/导入(Export/Import)利 ...
- RNA-seq流程需要进化啦!
RNA-seq流程需要进化啦! Posted on 2015年9月25日 Tophat 首次被发表已经是6年前 Cufflinks也是五年前的事情了 Star的比对速度是tophat的50倍,hisa ...
- 简单理解RNA-seq
简单理解RNA-seq 刘小泽 已关注 2018.10.17 23:51* 字数 1518 阅读 46评论 0喜欢 3 今天就当一个小故事看吧,看了statQuest,感觉讲的很棒,于是分享给大家原版 ...
- 1.1 Java 的概述
[什么是java]:Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是有SunMicrosystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaSE,Jav ...
- tensorflow初始化函数变更
变量初始化函数改变 老版本:initialize_all_variables()(2017-03-02之后删除) 新版本:global_variables_initializer()
- Windows-universal-samples学习笔记系列三:Navigation
Navigation Back Button Master/detail Navigation menu (XAML) Pivot Projection XHR, handling navigatio ...
- mysql 字符串数值计算 精度丢失
我进行了一些测试.truncate(abs('414')/100,2)truncate('414'/100,2)truncate('4.14',2)truncate('4.1400',2)都有精度丢失 ...
- Django的学习(四)———— admin
admin是django自带的一个管理者,由于自带所以直接对admin文件进行一个配置. 一.创建用户: python manage.py createsuperuser 创建合理的用户信息就可以在网 ...
- 网络编程之IO模型
IO模型的分类 blocking IO:阻塞IO nonblocking IO:非阻塞IO IO multiplexing:IO多路复用 signal driven IO:异步IO 通常情况下IO默认 ...