思路
烹饪时间 本质是离开时间减去进入时间,且 大于等于 (时间差不可能为负)。
因此所有可能的候选 只能是 的非负结果,只需统计这些候选值的出现次数,即可找到最优解。
代码
#include<bits/stdc++.h>
#define lcm(x,y) x/__gcd(x,y)*y
#define lb(x) (x&-x)
#define str to_string
#define endl '\n'
#define endr '\r'
#define emp_ '\0'
using namespace std;
#define int long long
using ll=long long;
namespace Fast{
namespace Fread{const int SZ=1<<20;char buf[SZ],*S,*TT;inline char getchar(){if(S==TT){TT=(S=buf)+fread(buf,1,SZ,stdin);if(S==TT)return endl;}return *S++;}}
namespace Fwrite{const int SZ=1<<20;char buf[SZ],*S=buf,*TT=buf+SZ;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==TT)flush();}struct NTR{~NTR(){flush();}} ztr;}
using namespace Fread;
using namespace Fwrite;
#define getchar Fread::getchar
#define putchar Fwrite::putchar
namespace FastIO{
struct R{
template<typename T>
R&operator>>(T&from){from=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')from=(from<<3)+(from<<1)+(c^48),c=getchar();from*=f;return*this;}
R&operator>>(double&from){from=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
R&operator>>(long double&from){from=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
R&operator>>(__float128&from){from=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
R&operator>>(char&c){c=getchar();while(c==' '||c==endl||c==endr)c=getchar();return*this;}
R&operator>>(char*str){int len=0;char c=getchar();while(c==' '||c==endl||c==endr)c=getchar();while(c!=' '&&c!=endl&&c!=endr)str[len++]=c,c=getchar();str[len]=emp_;return*this;}
R(){}
}cin;
struct W{
const int S=6;
typedef int M;
template<typename T>
W&operator<<(T from){if(from==0){putchar('0');return*this;}if(from<0)putchar('-'),from=-from;static short sta[40];short top=0;while(from>0)sta[++top]=from%10,from/=10;while(top>0)putchar(sta[top]+'0'),top--;return*this;}
W&operator<<(double from){if(from<0)putchar('-'),from=-from;M _=from;from-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
W&operator<<(long double from){if(from<0)putchar('-'),from=-from;M _=from;from-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
W&operator<<(__float128 from){if(from<0)putchar('-'),from=-from;M _=from;from-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
W&operator<<(char c){putchar(c);return*this;}
W&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}
W&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}
W(){}
}cout;
}
int max(register const int e,register const int b){register const int from(e),to(b);return(from>to?from:to);}
int min(register const int e,register const int b){register const int from(e),to(b);return(from>to?to:from);}
}
using namespace Fast;
#define cin FastIO::cin
#define cout FastIO::cout
const double EPS=1e-6,PAI=acos(-1.0);
const int MAX=5e5+5,mod=1e9+7,MOD=998244353;
map<int,int>mp; // 统计非负差值的出现次数
int n,m;
void solve(){
cin>>n>>m;
vector<int>t(n),ot(m);
for(int i=0;i<n;i++)cin>>t[i]; // in
for(int i=0;i<m;i++)cin>>ot[i]; // out
for(auto i:t)for(int j:ot)if(j-i>=0)mp[j-i]++; // 仅考虑非负的烹饪时间
int ma=0,ans=LLONG_MAX;
for(auto[t,cnt]:mp){
if(cnt>ma){ // 次数更大
ma=cnt;
ans=t;
}else if(cnt==ma&&t<ans)ans=t;// 次数相同
}
if(ans==LLONG_MAX)ans=0; // 无有效差值时
cout<<ans<<endl;
}
signed main(){
int t=1;
// cin>>t;
while(t--)solve();
return 0;
}
暂无评论