跳到正文

题解

题解:P12686 [KOI 2022 Round 1] 面包

ChidoriX约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟
题目
P12686

版本

  1. v1当前

题目传送门

解析

我们可以先筛选出所有能购买面包的店铺,然后在这些店铺中选择面包到货最快的店铺购买面包。也就是说,在满足 A≤BA \le B 条件的店铺中,输出 BB 值最小的那个。该过程使用 forfor 循环和 ifif 语句实现,时间复杂度为 O(N)O(N) 。

代码

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n;
int a[110],b[110];
signed main()
{
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i];
	int ans=LLONG_MAX;
	bool f=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]<=b[i])
		{
			ans=min(ans,b[i]);
			f=1;
		}
	}
	if(f==0) cout<<-1<<endl;
	else cout<<ans<<endl;
	return 0;
}

完结。

原文