P4266 [USACO18FEB]Rest Stops
题目描述
思路
·这题就是一个贪心,先将每个休息站按照美味值从大到小排序,然后从头到尾扫一遍,能吃就一直吃到FJ追上来为止。
·long long!long long!!long long!!!
代码
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int N=100010; long long l,n,v1,v2,ans,a2; struct no { long long a,b; } a[N]; bool cmp(no a,no b) { return a.b>b.b; } int main () { scanf("%lld%lld%lld%lld",&l,&n,&v1,&v2); for(long long i=1; i<=n; i++) scanf("%lld%lld",&a[i].a,&a[i].b); sort(a+1,a+1+n,cmp); for(long long i=1; i<=n; i++) if(a[i].a*v2+a2<=a[i].a*v1) { ans+=(a[i].a*v1-a[i].a*v2-a2)*a[i].b; a2=a[i].a*v1-a[i].a*v2; } printf("%lld\n",ans); return 0; }