跳到正文

题解

题解:P14336 [JOI2020 预选赛 R2] 草莓 / Strawberry

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

文章信息

发布
2025年10月29日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 1 分钟
题目
P14336

版本

  1. v2当前
  2. v1

终于找到了一道可以写题解的题了

思路:

  • 通过贪心求解。
  • 先假设每一棵草莓都不需要等待就成熟了。
  • 再枚举每一棵草莓的等待时间,取最大值。
  • 输出来回的距离加等待时间的最大值。

AC code:

C++
#include<bits/stdc++.h>
using namespace std;
int n,ans;
int e=0,maxx;
struct node
{
	int a,t;
}p[100010];
bool cmp(node x,node y)
{
	return x.a<y.a;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>p[i].a>>p[i].t;//读入
	}
	sort(p+1,p+1+n,cmp);//按距离排序
	ans=p[n].a*2;//来回的距离
	for(int i=1;i<=n;i++)
	{
		maxx=max(maxx,max(e,p[i].t-p[n].a-(p[n].a-p[i].a)));//取等待时间的最大值
	}
	cout<<ans+maxx<<endl;//输出
}

最后恳求管理员大大通过!

原文