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

CodeForces - 948C Producing Snow(优先队列)

时间:2018-07-26 21:12:57      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:size   queue   前缀   计算   mes   typedef   alt   ++   else   

题意:

n天。

每天你会堆一堆雪,体积为 v[i]。每天都有一个温度 t[i] 所有之前堆过的雪在第 i 天体积都会减少 t[i] 。

输出每天融化了的雪的体积。

 

技术分享图片

 

 

 

 

这个题的正解我怎么想都很难理解,但是慢慢理解了。

计算一个 t[i] 的前缀和 sum。

那么到第 j 天时,设第 i 堆雪融化的体积是 V,则 V = min (sum [ j ] - sum[ i-1], v[ i ] )

所以把 v[ i ] + sum[ i -1] 加入优先队列里,就可以只处理所有当天能化完的雪了。

若 sum[ j ] - v [i] + sum[i - i] <= 0,则证明第 i 堆雪在第 j 天能化完,对答案贡献为V。

否则对答案贡献为 t[i] 。

 

 

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

#define maxn 100000 + 1000
typedef long long LL;

int main()
{
    int n;
    scanf("%d", &n);

    int v[maxn], t[maxn];
    for (int i = 1; i <= n; i++)
        scanf("%d", &v[i]);

    LL sum[maxn];
    sum[0] = 0;

    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &t[i]);
        sum[i] = sum[i-1] + t[i];
    }

    priority_queue<LL, vector<LL>, greater<LL> > q;
    for (int i = 1; i <= n; i++)
    {
        q.push(v[i] + sum[i-1]);
        LL ans = 0;
        while(!q.empty())
        {
            LL x = q.top();
            if (sum[i]-x >= 0)//这堆雪第 i 天能够融化
            {
                ans += t[i] - (sum[i]-x); 
                q.pop();
            }
            else break; // 否则后面的都化不完了
        }
        ans += q.size() * t[i];

        printf("%lld%c", ans, i==n? \n: );
    }
}

 

CodeForces - 948C Producing Snow(优先队列)

标签:size   queue   前缀   计算   mes   typedef   alt   ++   else   

原文地址:https://www.cnblogs.com/ruthank/p/9374017.html

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