zoukankan      html  css  js  c++  java
  • luogu2827 蚯蚓

    题目大意

    本题中,我们将用符号[c]表示对c向下取整,例如:[3.0」= [3.1」= [3.9」=3。
    蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓。
    蛐蛐国里现在共有n只蚯蚓(n为正整数)。每只蚯蚓拥有长度,我们设第i只蚯蚓的长度为a_i(i=1,2,…,n),并保证所有的长度都是非负整数(即:可能存在长度为0的蚯蚓)。
    每一秒,神刀手会在所有的蚯蚓中,准确地找到最长的那一只(如有多个则任选一个)将其切成两半。神刀手切开蚯蚓的位置由常数p(是满足0< p<1的有理数)决定,设这只蚯蚓长度为x,神刀手会将其切成两只长度分别为[px]和x-[px]的蚯蚓。特殊地,如果这两个数的其中一个等于0,则这个长度为0的蚯蚓也会被保留。此外,除了刚刚产生的两只新蚯蚓,其余蚯蚓的长度都会增加q(是一个非负整常数)。
    蛐蛐国王知道这样不是长久之计,因为蚯蚓不仅会越来越多,还会越来越长。蛐蛐国王决定求助于一位有着洪荒之力的神秘人物,但是救兵还需要m秒才能到来……
    (m为非负整数)
    蛐蛐国王希望知道这m秒内的战况。具体来说,他希望知道:
    •m秒内,每一秒被切断的蚯蚓被切断前的长度(有m个数)
    •m秒后,所有蚯蚓的长度(有n+m个数)。
    蛐蛐国王当然知道怎么做啦!但是他想考考你……

    题解

      先将所有蚯蚓排序。随后用三个队列维护以下三类蚯蚓:队列1:没有被砍的蚯蚓;队列2:被砍的蚯蚓的前半段;队列3:被砍的蚯蚓的后半段。每次将砍掉的最长的蚯蚓,前半段放入队列2,后半段放入队列3,经过数学推导得,这三个队列内的值都是单调递减的。于是取最长的蚯蚓就在三个队列的队首取即可。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    using namespace std;
    
    #define ll long long
    const int MAX_N = 100010;
    ll A[MAX_N];
    
    struct Node
    {
        ll Len;
        int Time;
        queue<Node> *QIn;
    
        Node(){}
    
        Node (ll len, int time, queue<Node> *qIn):Len(len), Time(time), QIn(qIn){}
    };
    
    bool Cmp(const Node& a, const Node& b)
    {
        return a.Len > b.Len;
    }
    
    ll GetLen(queue<Node> *q, int time, int addLen)
    {
        Node temp[3];
        for (int i = 0; i < 3; i++)
        {
            temp[i].QIn = q + i;
            temp[i].Len = q[i].empty() ? 0 : q[i].front().Len + addLen * (time - q[i].front().Time - 1);
        }
        sort(temp, temp + 3, Cmp);
        if (temp[0].QIn->empty())
            return -1;
        ll ans = temp[0].Len;
        temp[0].QIn->pop();
        return ans;
    }
    
    int main()
    {
        int n, totTime, addLen, pu, pd, timeUnit;
        static queue<Node> q[3];
        scanf("%d%d%d%d%d%d", &n, &totTime, &addLen, &pu, &pd, &timeUnit);
        for (int i = 0; i < n; i++)
            scanf("%lld", A + i);
        sort(A, A + n);
        for (int i = n - 1; i >= 0; i--)
            q[0].push(Node(A[i], 0, q));
        for (int i = 1; i <= totTime; i++)
        {
            ll len = GetLen(q, i, addLen);
            if (i % timeUnit == 0)
                printf("%lld ", len);
            ll len1 = len * pu / pd;
            ll len2 = len - len1;
            q[1].push(Node(len1, i, q + 1));
            q[2].push(Node(len2, i, q + 2));
        }
        printf("
    ");
        int cnt = 0;
        ll len;
        while ((len = GetLen(q, totTime + 1, addLen)) && len != -1)
        {
            cnt++;
            if (cnt % timeUnit == 0)
                printf("%lld ", len);
        }
        printf("
    ");
        return 0;
    }
    

      

  • 相关阅读:
    浮动广告
    jQuery给table添加行和删除行
    oracle优化方式和sql标准
    使用JavaScript中的ActiveXObject填充并设置Excel格
    打印相关的js
    利用js导出Excel
    Oracle左外连接和右外连接的写法
    天气预报抓取的方法和源代码(包括从IP获取)
    algorithm
    ungetc
  • 原文地址:https://www.cnblogs.com/headboy2002/p/9874073.html
Copyright © 2011-2022 走看看