Baidu Star 2019 - a B preliminary round title Game

Topic Link

Meaning of the questions: the degree of bears playing a fun game. Hero of the game a number of standing axis, he can move on the number line, for each movement, he can go to the right or left to select one cell or two cells. Now he has to in order to complete  the n- the n-tasks, for task  i i, as long as he is in the interval  [a_i, b_i] [ A i , b i ] on, even if the completion of the task. Degree of Bear wanted to know, in order to complete all the tasks, at least you need to move many times? Degree, bears the initial position can be selected arbitrarily.

Ideas: the beginning of the consolidation range and then traverse the past, and then look left or right, it will all go in one direction and then seek sum accumulated in the opposite direction. Note that when the interval a point, must be added.

Feeling very simple idea, that is too much detail.

#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<cstdio>
#include<cmath>
#define ll long long
#define lowbit(x) x&(-x)
using namespace std;
struct point 
{
    ll x;
    ll y;
}a[100010];
int b[100010];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(b,0,sizeof(b));
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%lld%lld",&a[i].x,&a[i].y);
        }
        if(n==1)
        {    
            printf("0\n");
            continue;
        }
        ll sum1=0,sum2=0,sum=0;
        int m=0;
        ll r1=a[0].x;
        ll r2=a[0].y;
        int d;
        int flag=0;
        int isleft=2;
        int u=0;
        int k=0;
        for(int i=1;i<n;i++)
        {
            if(flag==0)
            {
                if(a[i].x>r2)
                {
                    m=r2;
                    d=r2-r1;
                    flag=1;
                    isleft=0;
                }
                else if(r1>a[i].y)
                {
                    m=r1;
                    d=r2-r1;
                    isleft=1;
                    flag=1;
                }
                else
                {
                    r1=max(r1,a[i].x);
                    r2=min(r2,a[i].y);
                }
            //    printf("m:::::%d\n",m);
            }
            if(flag==1)
            {
                if(a[i].x==a[i].y&&a[i].x!=m)
                {
                    if(a[i].x>m)
                    {
                        if((a[i].x-m)%2==0)
                        {
                            sum+=(a[i].x-m)/2;
                        }
                        else
                        {
                            sum+=(a[i].x-m)/2+1;
                        }
                        isleft=0;
                    }
                    else
                    {
                        if((m-a[i].y)%2==0)
                        {
                            sum+=(m-a[i].y)/2;
                        }
                        else
                        {
                            sum+=(m-a[i].y)/2+1;
                        }
                        isleft=1;
                    }
                    m=a[i].x;
                    
                }
                else
                {
                //    printf("m:%d\n",m);
                //    printf("%lld %lld\n",a[i].x,a[i].y);
                    if(a[i].x>m)
                    {
                        if(isleft==1)
                        {
                            if((m-a[i-1].y)%2==0)
                            {
                                sum+=(m-a[i-1].y)/2;
                            }
                            else
                            {
                                sum+=(m-a[i-1].y)/2+1;
                            }
                            m=a[i-1].y;    
                        }
                        isleft=0;
                    }
                    if(a[i].y<m)
                    {
                        if(isleft==0)
                        {
                            if((a[i-1].x-m)%2==0)
                            {
                                sum+=(a[i-1].x-m)/2;
                            }
                            else
                            {
                                sum+=(a[i-1].x-m)/2+1;
                            }
                            m=a[i-1].x;    
                        }
                        isleft=1;
                    }
                }
            //    printf("sum:%d\n",sum);
            }    
        }
        if(isleft==0)
        {
            if((a[n-1].x-m)%2==0)
            {
                sum+=(a[n-1].x-m)/2;
            }
            else
            {
                sum+=(a[n-1].x-m)/2+1;
            }
        }
        if(isleft==1)
        {
            if((a[n-1].y-m)%2==0)
            {
                sum+=(m-a[n-1].y)/2;
            }
            else
            {
                sum+=(m-a[n-1].y)/2+1;
            }
        }
        printf("%lld\n",sum);
    }
}

 

No, I do not know what was wrong. Perhaps the idea was wrong.

Guess you like

Origin www.cnblogs.com/2462478392Lee/p/11370536.html