# 洛谷4266 [usaco18feb]rest stops

## 奶牛贪心题

Posted by yjjr's blog on January 26, 2019

# 题目

## 题意简述

Farmer John和Bessie在爬山，高度为$L$，Farmer John需要$R_F$的时间爬一米，Bessie只需要$R_B$的时间爬一米，规定$R_B < R_F$（严格小于）。同时有$N$个休息站，Bessie可以决定在每个休息站休息多长时间（或者不休息），给出所有休息站的位置$X_i$和权值$C_i$，在一个休息站待$T$单位时间，答案可以获得$C_i\times T$的贡献值。请你在保证Bessie领先于Farmer John的情况下，最大化答案。

# 题解

## English

We can simply perform a greedy algorithm: stop at a rest stop which has the Maximal $C_i$ in this area you can arrived, and stay at this rest stop as long as possible. After that, find the next rest stop which also has the Maximal $C_j$ in next area. It’s supposed $P_j > P_i$. And also stay at rest stop $J$ as long as possible, until Farmer John is coming.

It’s easy to prove the greedy algorithm: If it didn’t stay at the maximal $C_i$ rest stop, then changed to another rest stop $C_k$, that means $C_k$ must be less than $C_i$, so that $C_k\times T < C_i\times T$. So that can’t be the best answer, so the greedy algorithm is correct.

# Code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=1e6+6;
ll l,n,f,b;
struct node{ll x,c;}a[maxn];
inline bool cmp(node a,node b){return a.c>b.c;}
int main(){