1. #include <bits/stdc++.h>
  2. #define N 300010
  3. #define PII pair<int, int>
  4. using namespace std;
  5.  
  6. typedef long long LL;
  7.  
  8. int n, m, a[N], c[N], t, d;
  9. LL ans = ;
  10.  
  11. priority_queue<PII, vector<PII>, greater<PII> > Q;
  12.  
  13. int main(){
  14. scanf("%d%d", &n, &m);
  15. for (int i = ; i <= n; i++){
  16. scanf("%d", &a[i]);
  17. }
  18.  
  19. for (int i = ; i <= n; i++){
  20. scanf("%d", &c[i]);
  21. Q.push(make_pair(c[i], i));
  22. }
  23.  
  24. for (int i = ; i <= m; i++){
  25. scanf("%d%d", &t, &d);
  26. if (d <= a[t]){
  27. a[t] -= d;
  28. printf("%lld\n", 1LL * d * c[t]);
  29. } else {
  30. bool flag = false;
  31. LL ans = 1LL * a[t] * c[t];
  32. d -= a[t];
  33. a[t] = ;
  34. while (!Q.empty()){
  35. while (!Q.empty() && a[Q.top().second] == ) Q.pop();
  36. if (Q.empty()) break;
  37. PII now = Q.top();
  38. if (d <= a[now.second]){
  39. a[now.second] -= d;
  40. ans += 1LL * d * now.first;
  41. flag = true;
  42. printf("%lld\n", ans);
  43. break;
  44. } else {
  45. ans += 1LL * a[now.second] * now.first;
  46. d -= a[now.second];
  47. a[now.second] = ;
  48. Q.pop();
  49. }
  50. }
  51.  
  52. if (!flag){
  53. puts("");
  54. }
  55. }
  56. }
  57. }

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

  1. #include<iostream>
  2. #include<queue>
  3. #include<algorithm>
  4. #define pii pair<int,int>
  5. using namespace std;
  6. int num[];
  7. int price[];
  8. typedef long long LL;
  9. priority_queue<pii,vector<pii>,greater<pii> > q;
  10. int main(){
  11. int n,m;
  12. cin>>n>>m;
  13. for(int i=;i<=n;i++)
  14. cin>>num[i];
  15. for(int i=;i<=n;i++){
  16. cin>>price[i];
  17. q.push(make_pair(price[i],i));
  18. }
  19. for(int i=;i<m;i++){
  20. int t,d;
  21. cin>>t>>d;
  22. LL sum=;
  23. int remain=d;
  24. if(num[t]>=d){
  25. sum+=1LL*d*price[t];
  26. num[t]-=d;
  27. remain=;
  28. }
  29. else{
  30. sum+=1LL*num[t]*price[t];
  31. remain-=num[t];
  32. num[t]=;
  33. }
  34. while(remain>){
  35. if(q.empty()){
  36. cout<<<<endl;
  37. break;
  38. }
  39. pii f=q.top();
  40. if(num[f.second]>remain){
  41. sum+=1LL*remain*f.first;
  42. num[f.second]-=remain;
  43. remain=;
  44. }
  45. else{
  46. sum+=1LL*num[f.second]*f.first;
  47. remain-=num[f.second];
  48. num[f.second]=;
  49. q.pop();
  50. }
  51. }
  52. if(remain==)
  53. cout<<sum<<endl;
  54. }
  55. return ;
  56. }

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. 对于devexpress gridview 内插图加加进度条等的一点解读

    如上图,gategory 加了小图标, 其他行内还有计算器,大图片   进度条等 using System; using System.Drawing; using System.Collection ...

  2. Oracle_高级功能(10) 备份恢复

    备份与恢复Oracle数据库有三种标准的备份方法,分别是导出/导入(EXP/IMP).热备份和冷备份.导出/导入是一种逻辑备份,冷备份和热备份是物理备份.一.导出/导入(Export/Import)利 ...

  3. RNA-seq流程需要进化啦!

    RNA-seq流程需要进化啦! Posted on 2015年9月25日 Tophat 首次被发表已经是6年前 Cufflinks也是五年前的事情了 Star的比对速度是tophat的50倍,hisa ...

  4. 简单理解RNA-seq

    简单理解RNA-seq 刘小泽 已关注 2018.10.17 23:51* 字数 1518 阅读 46评论 0喜欢 3 今天就当一个小故事看吧,看了statQuest,感觉讲的很棒,于是分享给大家原版 ...

  5. 1.1 Java 的概述

    [什么是java]:Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是有SunMicrosystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaSE,Jav ...

  6. tensorflow初始化函数变更

    变量初始化函数改变 老版本:initialize_all_variables()(2017-03-02之后删除) 新版本:global_variables_initializer()

  7. Windows-universal-samples学习笔记系列三:Navigation

    Navigation Back Button Master/detail Navigation menu (XAML) Pivot Projection XHR, handling navigatio ...

  8. mysql 字符串数值计算 精度丢失

    我进行了一些测试.truncate(abs('414')/100,2)truncate('414'/100,2)truncate('4.14',2)truncate('4.1400',2)都有精度丢失 ...

  9. Django的学习(四)———— admin

    admin是django自带的一个管理者,由于自带所以直接对admin文件进行一个配置. 一.创建用户: python manage.py createsuperuser 创建合理的用户信息就可以在网 ...

  10. 网络编程之IO模型

    IO模型的分类 blocking IO:阻塞IO nonblocking IO:非阻塞IO IO multiplexing:IO多路复用 signal driven IO:异步IO 通常情况下IO默认 ...