ACM Southwestern European Regional Programming Contest (SWERC) 2017--Cakey McCakeFace

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.

Input

  • Line 1: the number NN of times the entry detector was triggered.
  • Line 2: the number MM of times the exit detector was triggered.
  • Line 3: the NN integer timestamps at which the entry detector was triggered, sorted in ascending order, with no repetition, space-separated.
  • Line 4: the MM integer timestamps at which the exit detector was triggered, sorted in ascendingorder, with no repetition, space-separated.

Limits

  • 1 \le N, M \le 20001N,M2000;
  • each timestamp is between 00 and 1\ 000\ 000\ 0001 000 000 000 (inclusive).

Output

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
思路:对b数组中大于等于a数组的每个之间的差值插入multiset容器,然后从头到尾遍历,找到出现次数最多并且值最小的记录,最后打印即可。不要开大数组,因为可能会造成时间和内存超过,用容器或者自己写平衡二叉树的代码均可实现此过程,AC代码如下:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
typedef long long ll;
ll m,n;
ll a[2010];
ll b[2010];
multiset<ll> st;//定义容器,他将会对插入的数据自动从小到大排序 
int main()
{
	cin>>n;
	cin>>m;
	for(ll i=0;i<n;i++) cin>>a[i];
	for(ll i=0;i<m;i++) cin>>b[i];
	st.clear();//清空容器 
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<m;j++)
		{
			if(b[j]>=a[i]) st.insert(b[j]-a[i]);
		}
	}
	multiset<ll>::iterator j;//容器中定义迭代器的方式 
	ll x=-1;
	ll k=0;
	ll y=0;//一定要赋予初值,在上面的插入数据中,可能存在一个都没插入成功,此时下面循环将不会执行,最后打印0即可 
	ll sum=-1;
	for(j=st.begin();j!=st.end();j++)
	{
		if((*j)==x) k++;
		else if((*j)!=x)
		{
			if(k>sum)
			{
				sum=k;
				y=x;
			}
			x=(*j);
			k=1;
		}
	}
	cout<<y<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zhengxiangmaomao/article/details/79101013