【Codeforces257E】Greedy Elevator

本文介绍了一种基于四个set维护的电梯调度算法,该算法能够有效处理乘客上下楼的需求,并通过模拟关键时刻来确定电梯的移动方向。时间复杂度为O(nlogn),适用于处理大量乘客的情况。

题意:
有一个电梯,有n个人。
i个人会在ti时刻来到si层的电梯口,并想要去ti层。
每一个时刻,设pup为想要去楼上的在电梯中的人数与在楼上等待的人数之和,pdown为想要去楼下的在电梯中的人数与在楼下等待的人数之和,若puppdown则向上走一层,否则向下走一层。输出每个人到达目的地的时刻。

4个set维护四种人,关键时刻只有O(n)个,所以暴力模拟就好了。
时间复杂度O(nlogn)

#include <bits/stdc++.h>
#define gc getchar()
#define ll long long
#define N 100009
#define inf 0x3f3f3f3f
using namespace std;
int n,m;
ll Ans[N];
int read()
{
    int x=1;
    char ch;
    while (ch=gc,ch<'0'||ch>'9') if (ch=='-') x=-1;
    int s=ch-'0';
    while (ch=gc,ch>='0'&&ch<='9') s=s*10+ch-'0';
    return x*s;
}
struct passenger
{
    int t,s,f,pos;
    void scan(int i)
    {
        t=read(),s=read(),f=read(),pos=i;
    }
    bool operator <(const passenger &rhs) const
    {
        return t<rhs.t;
    }
}p[N],q[N];
struct node
{
    int id,pos;
    node(int id=0,int pos=0):id(id),pos(pos){}
    bool operator <(const node &rhs) const
    {
        return pos<rhs.pos;
    }
    bool operator >(const node &rhs) const
    {
        return pos>rhs.pos;
    }
};
multiset<node,less<node> > to_up,from_up;
multiset<node,greater<node> > to_down,from_down;
int pos=1,finish=0,id=1;
ll t=1;
void add_from()
{
    while (p[id].t==t)
    {
        if (pos==p[id].s)
        {
            if (p[id].f>pos) to_up.insert(node(p[id].pos,p[id].f));
            else to_down.insert(node(p[id].pos,p[id].f));
        }
        else
        {
            if (p[id].s>pos) from_up.insert(node(p[id].pos,p[id].s));
            else from_down.insert(node(p[id].pos,p[id].s));
        }
        id++;
    }
}
int main()
{
    n=read(),m=read();
    for (int i=1;i<=n;i++)
        p[i].scan(i),q[i]=p[i];
    sort(p+1,p+n+1);
    to_up.insert(node(0,inf)),from_up.insert(node(0,inf));
    to_down.insert(node(0,-inf)),from_down.insert(node(0,-inf));
    while (finish<n)
    {
        int size1=to_up.size()+from_up.size();
        int size2=to_down.size()+from_down.size();
        //puts("ok");
        if (size1==2&&size2==2)
        {
            if (id>n) break;
            t=p[id].t,add_from();
            continue;
        }
        if (size1>=size2)
        {
            node now1=*(to_up.begin());
            node now2=*(from_up.begin());
            if (id<=n&&p[id].t-t<now1.pos-pos&&p[id].t-t<now2.pos-pos)
            {
                pos+=p[id].t-t;
                t=p[id].t,add_from();
            }
            else
            {
                if (now1>now2) swap(now1,now2);
                t+=(ll)now1.pos-pos;
                pos=now1.pos;
                node now;
                while ((now=*(to_up.begin())).pos==pos)
                {
                    Ans[now.id]=t;
                    to_up.erase(to_up.find(now));
                    finish++;
                }
                while ((now=*(from_up.begin())).pos==pos)
                {
                    from_up.erase(from_up.find(now));
                    if (q[now.id].f>pos) to_up.insert(node(now.id,q[now.id].f));
                    else to_down.insert(node(now.id,q[now.id].f));
                }
                add_from();
            }
        }
        else
        {
            node now1=*(to_down.begin());
            node now2=*(from_down.begin());
            if (id<=n&&p[id].t-t<pos-now1.pos&&p[id].t-t<pos-now2.pos)
            {
                pos-=p[id].t-t;
                t=p[id].t,add_from();
            }
            else
            {
                if (now1<now2) swap(now1,now2);
                t+=(ll)pos-now1.pos;
                pos=now1.pos;
                node now;
                while ((now=*(to_down.begin())).pos==pos)
                {
                    Ans[now.id]=t;
                    to_down.erase(to_down.find(now));
                    finish++;
                }
                while ((now=*(from_down.begin())).pos==pos)
                {
                    from_down.erase(from_down.find(now));
                    if (q[now.id].f>pos) to_up.insert(node(now.id,q[now.id].f));
                    else to_down.insert(node(now.id,q[now.id].f));
                }
                add_from();
            }
        }
        //cout<<t<<" "<<pos<<endl;
    }
    for (int i=1;i<=n;i++)
        printf("%lld\n",Ans[i]);
    return 0;
}
### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值