zoukankan      html  css  js  c++  java
  • HDU 4302 Holedox Eating(优先队列或者线段树)

    Holedox Eating

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1830    Accepted Submission(s): 634


    Problem Description
    Holedox is a small animal which can be considered as one point. It lives in a straight pipe whose length is L. Holedox can only move along the pipe. Cakes may appear anywhere in the pipe, from time to time. When Holedox wants to eat cakes, it always goes to the nearest one and eats it. If there are many pieces of cake in different directions Holedox can choose, Holedox will choose one in the direction which is the direction of its last movement. If there are no cakes present, Holedox just stays where it is.
     
    Input
    The input consists of several test cases. The first line of the input contains a single integer T (1 <= T <= 10), the number of test cases, followed by the input data for each test case.The first line of each case contains two integers L,n(1<=L,n<=100000), representing the length of the pipe, and the number of events.
    The next n lines, each line describes an event. 0 x(0<=x<=L, x is a integer) represents a piece of cake appears in the x position; 1 represent Holedox wants to eat a cake.
    In each case, Holedox always starts off at the position 0.
     
    Output
    Output the total distance Holedox will move. Holedox don’t need to return to the position 0.
     
    Sample Input
    3 10 8 0 1 0 5 1 0 2 0 0 1 1 1 10 7 0 1 0 5 1 0 2 0 0 1 1 10 8 0 1 0 1 0 5 1 0 2 0 0 1 1
     
    Sample Output
    Case 1: 9 Case 2: 4 Case 3: 2
     
    Author
    BUPT
     
    Source
     
    Recommend
    zhuyuanchen520
     
     
     
     
    用线段树或者优先队列,multiset也是可以的。
    线段树的效率不是很高,可以优化
     
    优先队列:
    /*
    HDU 4302
    G++ 281ms 532K
    
    */
    
    #include<stdio.h>
    #include<queue>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    struct cmp
    {
        bool operator()(int x,int y)
        {
            return x>y;
        }
    };
    priority_queue<int,vector<int>,cmp>q;
    priority_queue<int>q2;
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int T;
        int L,n;
        int A,B;
        int iCase=0;
        scanf("%d",&T);
        while(T--)
        {
            iCase++;
            scanf("%d%d",&L,&n);
            while(!q.empty())q.pop();
            while(!q2.empty())q2.pop();
            int x=0;
            int ans=0;
            int t=1;
            while(n--)
            {
                scanf("%d",&A);
                if(A==0)
                {
                    scanf("%d",&B);
                    if(B>=x)q.push(B);
                    else q2.push(B);
                }
                else
                {
                    if(!q.empty()&&!q2.empty())
                    {
                        int temp1=q.top();
                        int temp2=q2.top();
                        if(temp1-x<x-temp2)
                        {
                            t=1;
                            ans+=q.top()-x;
                            x=q.top();
                            q.pop();
                        }
                        else if(temp1-x>x-temp2)
                        {
                            t=-1;
                            ans+=x-q2.top();
                            x=q2.top();
                            q2.pop();
                        }
                        else if(t==1)
                        {
                            ans+=q.top()-x;
                            x=q.top();
                            q.pop();
                        }
                        else
                        {
                            ans+=x-q2.top();
                            x=q2.top();
                            q2.pop();
                        }
                    }
                    else if(!q.empty())
                    {
                        t=1;
                        ans+=q.top()-x;
                        x=q.top();
                        q.pop();
                    }
                    else if(!q2.empty())
                    {
                        t=-1;
                        ans+=x-q2.top();
                        x=q2.top();
                        q2.pop();
                    }
                }
    
            }
            printf("Case %d: %d\n",iCase,ans);
        }
        return 0;
    }

    线段树:

    /*
    HDU 4302
    G++ 687ms  5652K
    */
    
    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    const int MAXN=100010;
    const int INF=0x3f3f3f3f;
    struct Node
    {
        int l,r;
        int t;
        int Min,Max;
    }segTree[MAXN*3];
    
    void Build(int i,int l,int r)
    {
        segTree[i].l=l;
        segTree[i].r=r;
        segTree[i].t=0;
        if(l==r)
        {
            segTree[i].Min=INF;
            segTree[i].Max=-1;
            return;
        }
        int mid=(l+r)>>1;
        Build(i<<1,l,mid);
        Build((i<<1)|1,mid+1,r);
        segTree[i].Max=max(segTree[i<<1].Max,segTree[(i<<1)|1].Max);
        segTree[i].Min=min(segTree[i<<1].Min,segTree[(i<<1)|1].Min);
    }
    void add(int i,int t)
    {
        if(segTree[i].l==t&&segTree[i].r==t)
        {
            segTree[i].Max=segTree[i].Min=t;
            segTree[i].t++;
            return;
        }
        int mid=(segTree[i].l+segTree[i].r)>>1;
        if(t<=mid)add(i<<1,t);
        else add((i<<1)|1,t);
        segTree[i].Max=max(segTree[i<<1].Max,segTree[(i<<1)|1].Max);
        segTree[i].Min=min(segTree[i<<1].Min,segTree[(i<<1)|1].Min);
    }
    void del(int i,int t)
    {
        if(segTree[i].l==t&&segTree[i].r==t)
        {
            segTree[i].t--;
            if(segTree[i].t==0)
            {
                segTree[i].Min=INF;
                segTree[i].Max=-1;
            }
    
            return;
        }
        int mid=(segTree[i].l+segTree[i].r)>>1;
        if(t<=mid)del(i<<1,t);
        else del((i<<1)|1,t);
        segTree[i].Max=max(segTree[i<<1].Max,segTree[(i<<1)|1].Max);
        segTree[i].Min=min(segTree[i<<1].Min,segTree[(i<<1)|1].Min);
    }
    int query1(int i,int l,int r)//查询最大值
    {
        if(segTree[i].l==l&&segTree[i].r==r)
        {
            return segTree[i].Max;
        }
        int mid=(segTree[i].l+segTree[i].r)>>1;
        if(r<=mid)return query1(i<<1,l,r);
        else if(l>mid) return query1((i<<1)|1,l,r);
        else  return max(query1(i<<1,l,mid),query1((i<<1)|1,mid+1,r));
    }
    
    int query2(int i,int l,int r)//查询最大值
    {
        if(segTree[i].l==l&&segTree[i].r==r)
        {
            return segTree[i].Min;
        }
        int mid=(segTree[i].l+segTree[i].r)>>1;
        if(r<=mid)return query2(i<<1,l,r);
        else if(l>mid) return query2((i<<1)|1,l,r);
        else  return min(query2(i<<1,l,mid),query2((i<<1)|1,mid+1,r));
    }
    
    int main()
    {
       // freopen("in.txt","r",stdin);
       // freopen("out.txt","w",stdout);
        int x;
        int T;
        int n;
        int m;
        int flag;
        scanf("%d",&T);
        int a,b;
        int iCase=0;
        while(T--)
        {
            iCase++;
            scanf("%d%d",&n,&m);
    
            Build(1,0,n);
            int flag=1;//往前的
            x=0;
            int ans=0;
    
            while(m--)
            {
                scanf("%d",&a);
                if(a==0)
                {
                    scanf("%d",&b);
                    add(1,b);
                }
                else
                {
                    int t1=query1(1,0,x);
                    int t2=query2(1,x,n);
                    if(t1==-1&&t2!=INF)
                    {
                        ans+=t2-x;
                        x=t2;
                        del(1,t2);
                        flag=1;
                    }
                    else if(t1!=-1&&t2==INF)
                    {
                        ans+=x-t1;
                        x=t1;
                        del(1,t1);
                        flag=-1;
                    }
                    else if(t1!=-1&&t2!=INF)
                    {
                        if(x-t1>t2-x)
                        {
                            ans+=t2-x;
                            x=t2;
                            del(1,t2);
                            flag=1;
                        }
                        else if(x-t1<t2-x)
                        {
                            ans+=x-t1;
                            x=t1;
                            del(1,t1);
                            flag=-1;
                        }
                        else
                        {
                            if(flag==1)
                            {
                                ans+=t2-x;
                                x=t2;
                                del(1,t2);
                                flag=1;
                            }
                            else
                            {
                                ans+=x-t1;
                                x=t1;
                                del(1,t1);
                                flag=-1;
                            }
                        }
                    }
    
    
                }
    
            }
            printf("Case %d: %d\n",iCase,ans);
    
        }
        return 0;
    }
  • 相关阅读:
    query_posts函数使用方法小结|wordpress技巧
    查看服务器被访问最大的ip
    Escape character is ‘^]’什么意思?怎么使用telnet
    telnet安装和使用教程
    开启了wpjam以后网站语言不能设置英文的解决方法
    wordpress获取当前页面链接
    woocommerce面包屑导航breadcrumb的修改
    如何将wordpress的the_title()进行大小写处理
    Sitemap Error : XML declaration allowed only at the start of the document解决方法
    mysql解析binlog日志
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2664289.html
Copyright © 2011-2022 走看看