zoukankan      html  css  js  c++  java
  • 刷水题 堆

    题目背景

    众所周知,熊本熊的洛谷大号是kakakaka。有的时候熊本熊会在洛谷上面刷水题,但是熊本熊并不知道该怎么计划好自己的时间,他想让你来帮他计划一下。

    题目大意

    熊本熊今天一时兴起想刷n道水题,熊本熊有m个大脑,每个大脑都只能独立工作,也就是说,熊本熊可以在同一时间做m道题。由于熊本熊刷的水题都是topcoder,codeforces,usaco,CCF上最新出的水题,所以在熊本熊刷一道水题时,下一道水题可能还没有出来。又因为熊本熊又不会什么奇技淫巧,所以他完成每一道题都有一个特定的时间。现在熊本熊想知道,对于每一个题目,他完成这道题目的时间。

    输入输出格式

    输入格式:

    第1行:四个整数N,M(1 ≤ n, m ≤5*10^5)

    第2~N+1行:每行两个数a,b,分别表示这道题目出版的时间和熊本熊做出这道题所需要的时间(1 ≤ a,b ≤ 10^9)

    输出格式:共N行:表示熊本熊完成第i道题目的时间。

    例数据

    input

    3 2
    1 5
    2 5
    3 5

    output

    6
    7
    11

    数据范围

    对于50%的数据 1 ≤ n,m ≤ 10

    对于100%的数据 1 ≤ n,m ≤ 5*10^5


    一开始看到这题,我是很开心的,因为这题有一个加强版,那题还有一个优先级要考虑,复杂的多。

    但我还是把这题给写挂了,只拿了40分。

    最后随便改改就对了,所以这题告诉我一定要造组大数据对拍,别以为做过了就自以为是。

    这题的思路就不再多加赘述了,很无脑、

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #define ll long long
    #define il inline
    #define db double
    using namespace std;
    il int gi()
    {
        int x=0,y=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')
            y=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*y;
    }
    il ll gl()
    {
        ll x=0,y=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')
            y=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*y;
    }
    struct task
    {
        ll begin,remain;
    }t[500045];
    ll heap[500045];
    int size;
    il void put(ll x)
    {
        heap[++size]=x;
        int now=size,next;
        while(now!=1)
        {
            next=now>>1;
            if(heap[next]<=heap[now])
            break;
            swap(heap[now],heap[next]);
            now=next;
        }
    }
    il void pop()
    {
        heap[1]=heap[size--];
        int now=1,next;
        while(now<<1<=size)
        {
            next=now<<1;
            if(heap[next+1]<heap[next]&&next<size)
            next++;
            if(heap[next]>=heap[now])
            break;
            swap(heap[now],heap[next]);
            now=next;
        }
    }
    int main()
    {
        freopen("water.in","r",stdin);
        freopen("water.out","w",stdout);
        int n=gi(),m=gi();
        for(int i=1;i<=n;i++)
        {
            t[i].begin=gl(),t[i].remain=gl();
            if(size<m)
            {
                ll now=t[i].begin+t[i].remain;
                put(now);
                printf("%lld
    ",now);
            }
            else
            if(heap[1]<=t[i].begin)
            {
                ll now=t[i].begin+t[i].remain;
                pop();
                put(now);
                printf("%lld
    ",now);
            }
            else
            {
                ll now=heap[1]+t[i].remain;
                pop();
                put(now);
                printf("%lld
    ",now);
            }
        }
        return 0;
    }

     

     

    PEACE
  • 相关阅读:
    你的灯亮着么阅读笔记2
    你的灯亮着么阅读笔记1
    梦断代码阅读笔记3
    梦断代码阅读笔记2
    梦断代码阅读笔记1
    百度搜索分析
    有多少1
    寻找“水王”问题
    站立会议
    买书的最低价格问题
  • 原文地址:https://www.cnblogs.com/gshdyjz/p/7396901.html
Copyright © 2011-2022 走看看