题解:P14336 [JOI2020 预选赛 R2] 草莓 / Strawberry
~终于找到了一道可以写题解的题了~
思路:
- 通过贪心求解。
- 先假设每一棵草莓都不需要等待就成熟了。
- 再枚举每一棵草莓的等待时间,取最大值。
- 输出来回的距离加等待时间的最大值。
AC code:
#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;//输出 }最后恳求管理员大大通过!