SGU 477 doors

477. Doors
Time limit per test: 1.5 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



It seems that the Museum of the Modern Technology is the only place where they don't celebrate Halloween! How can they be so far from all the mistical and mysterious? You can hardly believe that two weeks ago the Museum staff have destroyed the last detail that could bring with its ominous creak a bit of Halloween atmosphere to the realm of technology. They have replaced the old wooden doors with new automatic ones, and now they are scratching their heads over how to configure the doors. 

By the order of the Director, two automatic doors were purchased. An automatic door is characterized by parameter  t , called the  , which can be set to an integer value from 1 to 10 9  during the door installation. Then the door functions on the following principle. If a person passes through the door at time  p , the door opens at time  p  -  t  and closes at time  p  +  t . There is an exceptional case when several people go in a row with a time interval not exceeding 2  t  between any two consecutive people. In this case the door opens only once,  t  seconds before the first person in the row, and it closes  t  seconds after the last person in the row has passed through it. It is very important to set the optimal values of the door parameters. On the one hand, if the doors open and close too often, it will annoy visitors. On the other hand, if both doors stay opened for a long time, visitors can get cold.

More formally, two lists of time moments are given. At the moments  p 1  <  p 2  <... <  p n  people have passed through the first door, and at the moments  q 1  <  q 2  <... <  q m  people have passed through the second one. The task is to use the given statistics to find the optimal   for the doors —  t 1  for the first door and  t 2  for the second one that satisfy the following conditions:

  • The total number of openings of the doors must be minimal possible.
  • There is no continuous interval of time that both doors are opened during this interval and its length exceeds the given value d

    Input
    The first line of the input contains three integers nm and d (1 ≤ nm ≤ 5000, 1 ≤ d ≤ 109). The second line contains numbers pi, and the third line contains numbers qi, given in the ascending order (1 ≤ piqi ≤ 109).

    Output
    Output two integers t1 and t2, separated by a single space. If there are multiple solutions, output any. If there is no solution, output "
    No solution
    ". 

    Example(s)
    sample input
    sample output
    3 2 4
    1 6 13
    7 11
    
    3 2
    


    题意:两个门分别设定t1,t2两个值,使得满足某些条件。

    题解:首先,门的t值的一定为1或者(相邻两个间隔+1) /2。那么窝们可以枚举。但是判断两个门的公共时间是最少是o(n)的,那么如果n^2枚举坑定会跪。那么仔细一看其实o(n)枚举,给出源代码。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<string>
    #include<algorithm>
    #include<iostream>
    #include<vector>
    #include<queue>
    #include<map>
    #include<set>
    #include<stack>
    
    #define msn(x) (memset((x),0,sizeof((x))))
    #define msx(x) (memset((x),0x7f,sizeof((x))))
    #define fuck(x) cerr << #x << " <- " << x << endl
    #define acer cout<<"sb"<<endl
    typedef int ll;
    using namespace std;
    int n,m,d;
    const int maxn=5003;
    int p[maxn],q[maxn];
    vector<int>cmp,cmq;
    struct node
    {
        ll a,b;
    };
    vector<node>t,s;
    node g;
    int ansa,ansb,ans;
    node mp(ll a,ll b)
    {
        node re;
        re.a=a,re.b=b;
        return re;
    }
    void pre1(int w)
    {
        s.clear();
        for(int i=0;i<n;i++)
        {
            ll beg=p[i]-w;
            while(i+1<n&&p[i+1]-2*w<=p[i])i++;
            ll fin=p[i]+w;
            s.push_back(mp(beg,fin));
        }
    }
    void pre2(int w)
    {
        t.clear();
        for(int i=0;i<m;i++)
        {
            ll beg=q[i]-w;
            while(i+1<m&&q[i+1]-2*w<=q[i])i++;
            ll fin=q[i]+w;
            t.push_back(mp(beg,fin));
        }
    }
    
    inline ll getintersection(node &a,node &b)
    {
        ll minn=max(a.a,b.a);
        ll maxx=min(a.b,b.b);
        return max(0,maxx-minn);
    }
    bool solve(int a,int b)
    {
        pre2(cmq[b]);
        int j=0;
        for(int i=0;i<s.size();i++)
        {
           g=s[i];
           while(j<t.size())
           {
               ll ans=getintersection(g,t[j]);
               if(ans>d)return 0;
               if(j==t.size()-1||s[i].b<t[j].b)break;
               j++;
           }
        }
        if(s.size()+t.size()<ans)
        {
            ansa=cmp[a];
            ansb=cmq[b];
            ans=s.size()+t.size();
        }
        return 1;
    }
    int main()
    {
        scanf("%d %d %d",&n,&m,&d);
        ans=10000000;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&p[i]);
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d",&q[i]);
        }
        for(int i=0;i+1<n;i++)
        {
            cmp.push_back((p[i+1]+1-p[i])/2);
        }
        for(int i=0;i+1<m;i++)
        {
            cmq.push_back((q[i+1]+1-q[i])/2);
        }
        cmp.push_back(1);
        cmq.push_back(1);
        sort(cmp.begin(),cmp.end());
        cmp.erase(unique(cmp.begin(),cmp.end()),cmp.end());
        sort(cmq.begin(),cmq.end());
        cmq.erase(unique(cmq.begin(),cmq.end()),cmq.end());
        pre1(1);pre2(1);
        if(!solve(0,0))
        {
            printf("No solution\n");
            return 0;
        }
        int sz=cmq.size()-1;
        for(int i=0;i<cmp.size();i++)
        {
            pre1(cmp[i]);
            while(sz>=0&&!solve(i,sz))sz--;
            if(sz<0)break;
        }
        printf("%d %d\n",ansa,ansb);
        return 0;
    }
    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值