【P2827 蚯蚓】分析+乱搞

P2827 蚯蚓

题目链接:

https://www.luogu.org/problem/P2827

Description

给你 n n 个线段,每次操作把最长的线段 L L 拿出来分成 p × L \lfloor p\times L \rfloor L p × L L - \lfloor p\times L \rfloor 两部分再放回去,而且除了被操作的线段其他线段的长度都加 q q ,一共m回合,问每回合被操作的线段操作前的长度,和最终每条线段的长度。

Input

1 n 1 0 5 1 \leq n \leq 10^5
0 m 7 1 0 6 0 \leq m \leq 7*10^6
0 < u < v 1 0 9 0 < u < v \leq 10^9
0 q 200 0 \leq q \leq 200
1 t 71 1 \leq t \leq 71

Output

输出1:每t次操作输出一次被操作线段的长度。
输出2:将最终所有线段长度从大到小排序,输出第 t t 大,第 2 t 2*t 大…

Sample Input

3 7 1 1 3 1
3 3 2

Sample Output

3 4 4 4 5 5 6
6 6 6 5 5 4 4 3 2 2

Hint

在神刀手到来前:3只蚯蚓的长度为3,3,2。

1秒后:一只长度为3的蚯蚓被切成了两只长度分别为1和2的蚯蚓,其余蚯蚓的长度增加了1。最终4只蚯蚓的长度分别为(1,2),4,3。括号表示这个位置刚刚有一只蚯蚓被切断

2秒后:一只长度为4的蚯蚓被切成了1和3。5只蚯蚓的长度分别为:2,3,(1,3),4。

3秒后:一只长度为4的蚯蚓被切断。6只蚯蚓的长度分别为:3,4,2,4,(1,3)。

4秒后:一只长度为4的蚯蚓被切断。7只蚯蚓的长度分别为:4,(1,3),3,5,2,4。

5秒后:一只长度为5的蚯蚓被切断。8只蚯蚓的长度分别为:5,2,4,4,(1,4),3,5。

6秒后:一只长度为5的蚯蚓被切断。9只蚯蚓的长度分别为:(1,4),3,5,5,2,5,4,6。

7秒后:一只长度为6的蚯蚓被切断。10只蚯蚓的长度分别为:2,5,4,6,6,3,6,5,(2,4)。所以,7秒内被切断的蚯蚓的长度依次为3,4,4,4,5,5,6。7秒后,所有蚯蚓长度从大到小排序为6,6,6,5,5,4,4,3,2,2

题解:

首先考虑 n l o g n nlogn 的做法,就是维护一个优先队列,让这个优先队列里的所有值都等于真实值减去 T × q T \times q , T T 在这里表示切割蚯蚓的次数。
这样我们每次只需要在优先队列里找出最大值即可,设取出的值为x,那么他的真实值应该为 x + T × q x+T\times q
我们按照真实值进行切分,再把得到的两个长度减去当前的 T q T*q 放回优先队列即可。
这样就可以做到nlogn的复杂度,但是这个复杂度不足以通过本题。
之后我们发现,每次进行切割的形成的两个段的长度一定小于之前切割得到的两个段的长度的当前长度,这个仔细想一下就能理解。
设第X次切割的长度为 X 1 X_1 ,设第X+1次切割的长度为 X 2 X_2 ,那么可以知道 X 1 X 2 q X_1 \ge X_2 -q
p = u / v p = u/v
第一次切割得到的两个长度分别为 p X 1 , ( 1 p ) X 1 p*X_1,(1-p)*X_1
第二次切割得到的两个长度分别为 p X 2 , ( 1 p ) X 2 p*X_2,(1-p)*X_2
而 第二次切割时,X_1得到的两个段都会+q,那么长度就变为 p X 1 + q , ( 1 p ) X 1 + q p*X_1+q,(1-p)*X_1+q
由于 X 1 + q X 2 X_1+q \ge X_2
所以有:
p X 1 + p q p X 2 p*X_1+p*q \ge p*X_2
( 1 p ) X 1 + ( 1 p ) q ( 1 p ) X 2 (1-p)*X_1+(1-p)*q \ge(1-p)*X_2
所以有:
p X 1 + q p X 2 p*X_1+q \ge p*X_2
( 1 p ) X 1 + q ( 1 p ) X 2 (1-p)*X_1+q \ge(1-p)*X_2
所以可以知道,每次进行切割的形成的两个段的长度一定小于之前切割得到的两个段的长度的当前长度。
那么我们现在只需要维护三个队列即可。
第一个队列存储还未切割的线段,第二个队列存储切割后较长的那部分线段,第三个队列存储切割后较短的那部分线段,由于每个队列都是递减的,每次比较三个部分的队首元素进行切割即可,注意这里同样需要维护队列中的每个元素都是 T q -T*q 意义下的。

代码

TLE的mlogn代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<time.h>
#include<math.h>
using namespace std;

//***********************IO**********************************
namespace fastIO
{
    #define BUF_SIZE 100000
    #define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc()
    {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if (p1==pend)
        {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if (pend==p1)
            {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch)
    {
        return ch==' '|ch=='\n'||ch=='\r'||ch=='\t';
    }
    inline void read(int &x)
    {
        bool sign=0;
        char ch=nc();
        x=0;
        for (; blank(ch); ch=nc());
        if (IOerror)return;
        if (ch=='-')sign=1,ch=nc();
        for (; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if (sign)x=-x;
    }
    #undef OUT_SIZE
    #undef BUF_SIZE
};
using namespace fastIO;
//************************************************************************

#define ok cout<<"OK"<<endl;
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl;
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl;
#define print(a,n) for(int i=1;i<=n;i++) cout<<a[i]<<" ";cout<<endl;
#define pb push_back
#define Fi first
#define Se second
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>

const double eps = 1e-8;
const double PI = acos(-1.0);
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e5+10;
int a[maxn];
priority_queue<ll> pq;
int main()
{
    //freopen("D.in","r",stdin);
    int n,m,q,u,v,t;
    read(n),read(m),read(q);
    read(u),read(v),read(t);
    for(int i=1;i<=n;i++) read(a[i]);
    vector<int> ans1,ans2;
    ans1.clear();
    ans2.clear();
    for(int i=1;i<=n;i++) pq.push(a[i]);
    ll cnt=0;
    ll T=0;
    while(m--)
    {
        ll tp=pq.top();
        pq.pop();
        tp=tp+cnt;
        cnt+=q;
        T++;
        if(T%t==0) ans1.push_back(tp);
        ll a=tp*u/v;
        ll b=tp-a;
        pq.push(a-cnt);
        pq.push(b-cnt);
    }
    T=0;
    while(!pq.empty())
    {
        ll tp=pq.top();
        pq.pop();
        T++;
        if(T%t==0) ans2.push_back(tp+cnt);
    }
    for(int i=0;i<ans1.size();i++) printf("%d%c",ans1[i],i==(int)ans1.size()-1?'\n':' ');
    if(ans1.empty()) puts("");
    for(int i=0;i<ans2.size();i++) printf("%d%c",ans2[i],i==(int)ans2.size()-1?'\n':' ');
    if(ans2.empty()) puts("");
    return 0;
}

O(m)的代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<time.h>
#include<math.h>
using namespace std;


//***********************IO**********************************
namespace fastIO
{
    #define BUF_SIZE 100000
    #define OUT_SIZE 100000
    bool IOerror=0;
    inline char nc()
    {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if (p1==pend)
        {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if (pend==p1)
            {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch)
    {
        return ch==' '|ch=='\n'||ch=='\r'||ch=='\t';
    }
    inline void read(int &x)
    {
        bool sign=0;
        char ch=nc();
        x=0;
        for (; blank(ch); ch=nc());
        if (IOerror)return;
        if (ch=='-')sign=1,ch=nc();
        for (; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
        if (sign)x=-x;
    }
    #undef OUT_SIZE
    #undef BUF_SIZE
};
using namespace fastIO;
//************************************************************************

#define ok cout<<"OK"<<endl;
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl;
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl;
#define print(a,n) for(int i=1;i<=n;i++) cout<<a[i]<<" ";cout<<endl;
#define pb push_back
#define Fi first
#define Se second
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>

const double eps = 1e-8;
const double PI = acos(-1.0);
const int Mod = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 1e7+10;
int a[maxn];
ll ans1[maxn],ans2[maxn],c1,c2;
queue<ll> que[3];//que1->未切割  que2->切割后长的  que3->切割后短的
int main()
{
    //freopen("D.in","r",stdin);
    int n,m,q,u,v,t;
    read(n),read(m),read(q);
    read(u),read(v),read(t);
    for(int i=1;i<=n;i++) read(a[i]);
    ll cnt=0;
    ll T = 0;
    sort(a+1,a+1+n);
    for(int i=n;i>=1;i--) que[0].push(a[i]);
    while(m--)
    {
        ll mx=-1,flag=-1;
        for(int i=0;i<3;i++)
        {
            if(que[i].empty()) continue;
            int tp=que[i].front()+cnt;
            if(tp>mx) mx=tp,flag=i;
        }
        que[flag].pop();
        ll a=mx*u/v;
        ll b=mx-a;
        cnt+=q;
        if(a<b) swap(a,b);
        que[1].push(a-cnt),que[2].push(b-cnt);
        ++T;
        if(T%t==0) ans1[++c1]=mx;
    }
    for(int i=0;i<3;i++)
    {
        while(!que[i].empty())
        {
            int tp=que[i].front();
            ans2[++c2]=tp+cnt;
            que[i].pop();
        }
    }
    sort(ans2+1,ans2+1+c2,greater<int>());
    for(int i=1;i<=c1;i++) printf("%lld ",ans1[i]);
    puts("");
    for(int i=t;i<=c2;i+=t) printf("%lld ",ans2[i]);
    puts("");
    return 0;
}

发布了299 篇原创文章 · 获赞 117 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_38891827/article/details/101275385
今日推荐