2018组队训练赛第二十场.中石油.5165.Cakey McCakeFace

问题 A: Cakey McCakeFace

时间限制: 10 Sec   内存限制: 128 MB
提交: 362   解决: 48
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

Cakey McCakeFace’s signature pastry, the Unknowable Cake, is baked daily in their Paris facility.
The make-or-break trick for this cake is the cooking time, which is a very well-kept secret. Eve, the well-known spy, wants to steal this secret, and your job is to help her.
Cakes are cooked in a single huge oven that has exactly one front and one back door. The uncooked cakes are inserted through the front door. After the exact and very secret cooking time has passed, the cakes exit the oven through the back door. Only one cake can go through the front or back door at any given time.
Eve has secretly installed detectors at the front and back of the oven. They record a signal every time a cake passes through the doors. A cake will therefore trigger the entry detector at some time t when it goes through the front door, and then trigger the exit detector at time exactly t + cooking_time when it goes through the back door (all cakes at Cakey McCakeFace are always perfectly cooked).
After a few days, she receives two sets of timestamps (in ms) corresponding to entry and exit detectors. Unfortunately, the detectors  are faulty: they are sometimes triggered when no cake has passed, or they may fail to be triggered when a cake passes. Eve noticed that she could make a good guess of the secret cooking_time by finding the time difference that maximizes the number of  correspondences of entry and exit detection times. Help Eve compute this.

输入

• Line 1: the number N of times the entry detector was triggered.
• Line 2: the number M of times the exit detector was triggered.
• Line 3: the N integer timestamps at which the entry detector was triggered, sorted in ascending
order, with no repetition, space-separated.
• Line 4: the M integer timestamps at which the exit detector was triggered, sorted in ascending
order, with no repetition, space-separated.
Limits
• 1≤N, M≤2 000;
• each timestamp is between 0 and 1 000 000 000 (inclusive).

输出

A single integer: your best guess of the secret cooking_time, the (positive or zero) time difference that maximizes the number of correspondences of entry and exit detection times. If multiple such values exist, the smallest one should be returned.

样例输入

5
5
0 10 12 20 30
1 5 17 27 50

样例输出

5

#include<bits/stdc++.h>
using namespace std;
int a[2006],b[2006];
vector<int>v;
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        int cnt=0;
        v.clear();
        for(int i=1; i<=n; i++)
            cin>>a[i];
        for(int i=1; i<=m; i++)
            cin>>b[i];
        for(int i=1; i<=n; i++){
            for(int j=1; j<=m; j++){
                if(b[j]>a[i]){
                    v.push_back(b[j]-a[i]);
                    cnt++;
                }
            }
        }
        sort(v.begin(),v.begin()+cnt);
        int maxn=0,p=1000000005,t=1;
        for(int i=1; i<cnt; i++){
            if(v[i]==v[i-1]){
                t++;
            }else{
                if(t>maxn){
                    maxn=t;
                    p=v[i-1];
                }
                t=1;
            }
        }
        if(cnt==1){
            cout<<v[0]<<endl;
        }else if(cnt==0){
            cout<<0<<endl;
        }
        else{
            if(t>maxn){
                maxn=t;
                p=v[cnt-1];
            }
            cout<<p<<endl;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值