码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #536 (Div. 2) B. Lunar New Year and Food Ordering

时间:2019-02-13 12:25:49      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:lse   pac   color   ice   lun   code   bre   col   turn   

#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 = 0;

priority_queue<PII, vector<PII>, greater<PII> > Q;

int main(){
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++){
        scanf("%d", &a[i]);
    }
    
    for (int i = 1; i <= n; i++){
        scanf("%d", &c[i]);
        Q.push(make_pair(c[i], i));
    }
    
    for (int i = 1; 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] = 0;
            while (!Q.empty()){
                while (!Q.empty() && a[Q.top().second] == 0) 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] = 0;
                    Q.pop();
                }
            }
            
            if (!flag){
                puts("0");
            }
        }
    }
}

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

 

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

 

Codeforces Round #536 (Div. 2) B. Lunar New Year and Food Ordering

标签:lse   pac   color   ice   lun   code   bre   col   turn   

原文地址:https://www.cnblogs.com/albert67/p/10369000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!