codeforces Rest Stops

这是一道关于两个人徒步登山的问题,其中一人可以沿途在休息站停留并享用美味的草。题目要求最大化享受草的总价值,同时确保不会落后于走得更快的人。通过贪心策略,对休息站的草的价值进行排序,优先考虑最高价值的休息站,以获取最大的总价值。
摘要由CSDN通过智能技术生成

题面

outputstandard output
Farmer John and his personal trainer Bessie are hiking up Mount Vancowver. For their purposes (and yours), the mountain can be represented as a long straight trail of length L meters (1≤L≤106). Farmer John will hike the trail at a constant travel rate of rF seconds per meter (1≤rF≤106). Since he is working on his stamina, he will not take any rest stops along the way.

Bessie, however, is allowed to take rest stops, where she might find some tasty grass. Of course, she cannot stop just anywhere! There are N rest stops along the trail (1≤N≤105); the i-th stop is xi meters from the start of the trail (0<xi<L) and has a tastiness value ci (1≤ci≤106). If Bessie rests at stop i for t seconds, she receives ci⋅t tastiness units.

When not at a rest stop, Bessie will be hiking at a fixed travel rate of rB seconds per meter (1≤rB≤106). Since Bessie is young and fit, rB is strictly less than rF.

Bessie would like to maximize her consumption of tasty grass. But she is worried about Farmer John; she thinks that if at any point along the hike she is behind Farmer John on the trail, he might lose all motivation to continue!

Help Bessie find the maximum total tastiness units she can obtain while making sure that Farmer John completes the hike.

Input
The first line of input contains four integers: L, N, rF, and rB. The next N lines describe the rest stops. For each i between 1 and N, the i+1-st line contains two integers xi and ci, describing the position of the i-th rest stop and the tastiness of the grass there.

It is guaranteed that rF>rB, and 0<x1<…<xN<L. Note that rF and rB are given in seconds per meter!

Output
A single integer: the maximum total tastiness units Bessie can obtain.

input
10 2 4 3
7 2
8 1
output
15

Note
In this example, it is optimal for Bessie to stop for 7 seconds at the x=7 rest stop (acquiring 14 tastiness units) and then stop for an additional 1 second at the x=8 rest stop (acquiring 1 more tastiness unit, for a total of 15 tastiness units).

题意

两个人走路长L,路上有N个休息站,给出两人的速度,rF一定大于rB,注意这里的单位是秒每米,即一米走多少秒,那么肯定越少速度越快了。
速度快的那位可以在休息站的时候停下来吃草,给出每个休息站 的位置和草的价值,在休息站每停1s就可以获得该价值的草。但你要保证不能掉到速度慢那位的后面。

分析

贪心就完事,排序价值,在最大价值前面的肯定不吃,攒时间到最大价值那里再吃,然后再看次大价值是否在最大价值之后,是就重复刚才的操作,否则再往下看,直到最后一个。

代码

#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long ll;
const int maxn=1e5+5;
struct node{
	int pos,c;
}stop[maxn];
bool cmp(node a,node b){
	if(a.c!=b.c) return a.c>b.c;
	return a.pos>b.pos;
}
int main(){
	int l,n,rf,rb,i;
	scanf("%d%d%d%d",&l,&n,&rf,&rb);
	for(i=0;i<n;i++) scanf("%d %d",&stop[i].pos,&stop[i].c);
	sort(stop,stop+n,cmp);
	int now=0;
	ll ans=0;
	for(i=0;i<n;i++){
		if(stop[i].pos>now){
			ans+=(ll)(stop[i].pos-now)*(rf-rb)*stop[i].c;
			now=stop[i].pos;
		}
	}
	printf("%lld\n",ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值