跳到正文

题解

题解:P15218 [SWERC 2017] Cakey McCakeFace

Sky_distant约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年2月9日
更新
2026年5月9日
篇幅
约 2 分钟
题目
P15218

版本

  1. v2当前
  2. v1

题目分析

  • 第一行进入探测器触发的次数 NN,第二行离开探测器的次数 MM,第三行有 NN 个整数为蛋糕进入探测器的时间,第四行有 MM 个整数为蛋糕离开探测器的时间。
  • 使用 mapmap 映射,计算每个进入探测器时间和每个离开探测器的时间(除去离开时间比进入时间早的情况)。
  • 最后遍历一遍 mapmap 找到出现最多的,时间最小的值并记录下来,就是最佳答案。

代码实现

C++
#include<bits/stdc++.h>
using namespace std;
inline void solve(){
	int n,m;
	cin>>n>>m;
	int a[n+10],b[m+10],maxx=0,maxn=0;
	for(int i=0;i<n;i++) cin>>a[i];
	for(int i=0;i<m;i++) cin>>b[i];
	map<int,int> mp;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(b[j]>=a[i])mp[b[j]-a[i]]++;
		}
	}
	map<int,int>::iterator it;
	for(it=mp.begin();it!=mp.end();it++){
		int t=it->first,num=it->second;
		if(num>maxx){
			maxx=num;
			maxn=t;
		}
		if(num==maxx&&t<maxn){
			maxn=t;
		}
	}
	cout<<maxn<<endl;
}
int main(){
	solve();
	return 0;
}//zhangtianyuan123
原文