洛谷 P4266 [USACO18FEB] Rest Stops

题意翻译

Farmer John和他的私人教练Bessie正在徒步攀登温哥牛山。基于他们的目的(也是你的目的),这座山可以用一条长为LLL米(1≤L≤1061 \leq L \leq 10^61≤L≤106)的长直路径表示。Farmer John会沿着这条路径以每米rFr_FrF​秒(1≤rF≤1061 \leq r_F \leq 10^61≤rF​≤106)的固定速度攀登。由于他正在训练他的耐力,他在途中不会进行任何的休息。 然而Bessie可以在休息站休息,在那里她能够找到一些美味的嫩草。当然,她也不能在任何地方都休息!在路径上总共有NNN个休息站(1≤N≤1051 \leq N \leq 10^51≤N≤105);第iii个休息站距离路径的起点xix_ixi​米(0<xi<L0 < x_i < L0<xi​<L),美味值为cic_ici​(1≤ci≤1061 \leq c_i \leq 10^61≤ci​≤106)。如果Bessie在休息站iii休息了ttt秒,她能够得到ci⋅tc_i \cdot tci​⋅t个美味单位。

不在休息站的时候,Bessie会以每米rBr_BrB​秒(1≤rB≤1061 \leq r_B \leq 10^61≤rB​≤106)的固定速度攀登。由于Bessie年轻而健康,rBr_BrB​严格小于rFr_FrF​。

Bessie想要吃到最多的美味嫩草。然而她也担心Farmer John;她认为如果在任何时候她位于Farmer John身后,Farmer John可能就会失去前进的动力了!

帮助Bessie求出,在确保Farmer John能够完成登山的情况下,她能够获得的最多的美味单位。

输入格式(文件名:reststops.in): 输入的第一行包含四个整数:LLL,NNN,rFr_FrF​,以及rBr_BrB​。下面NNN行描述了休息站。对于111至NNN之间的每一个iii,第i+1i+1i+1行包含了两个整数xix_ixi​和cic_ici​,描述了第iii个休息站的位置和那里的草的美味值。 输入保证rF>rBr_F > r_BrF​>rB​,并且0<x1<⋯<xN<L0 < x_1 < \dots < x_N < L 0<x1​<⋯<xN​<L。注意rFr_FrF​和rBr_BrB​的单位为秒每米!

输出格式(文件名:reststops.out): 输出一个整数:Bessie可以获得的最多的美味单位。

题目描述

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 LLL meters (1≤L≤1061 \leq L \leq 10^61≤L≤106). Farmer John will hike the trail at a constant travel rate of rFr_FrF​ seconds per meter (1≤rF≤1061 \leq r_F \leq 10^61≤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 NNN rest stops along the trail (1≤N≤1051 \leq N \leq 10^51≤N≤105); the iii-th stop is xix_ixi​ meters from the start of the trail (0<xi<L0 < x_i < L0<xi​<L) and has a tastiness value cic_ici​ (1≤ci≤1061 \leq c_i \leq 10^61≤ci​≤106). If Bessie rests at stop iii for ttt seconds, she receives ci⋅tc_i \cdot tci​⋅t tastiness units.

When not at a rest stop, Bessie will be hiking at a fixed travel rate of rBr_BrB​ seconds per meter (1≤rB≤1061 \leq r_B \leq 10^61≤rB​≤106). Since Bessie is young and fit, rBr_BrB​ is strictly less than rFr_FrF​.

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.

输入输出格式

输入格式:

 

The first line of input contains four integers: LLL, NNN, rFr_FrF​, and rBr_BrB​. The next NNN lines describe the rest stops. For each iii between 111 and NNN, the i+1i+1i+1-st line contains two integers xix_ixi​ and cic_ici​, describing the position of the iii-th rest stop and the tastiness of the grass there. It is guaranteed that rF>rBr_F > r_BrF​>rB​, and 0<x1<⋯<xN<L0 < x_1 < \dots < x_N < L 0<x1​<⋯<xN​<L. Note that rFr_FrF​ and rBr_BrB​ are given in seconds per meter!

 

输出格式:

 

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

 

输入输出样例

输入样例#1:

10 2 4 3
7 2
8 1

输出样例#1:

15

说明

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

Problem credits: Dhruv Rohatgi

 

题目链接

 

一道很简单的贪心题。

既然给出了速度(注意题目中给出的速度是秒/米而不是米/秒),且在每个休息站能够停下的时间无限,那么这道题就从是贪心了(起初我还想了半天的DP)。

首先将每个休息站按照权值从大到小排序(如果权值相等就按照坐标从小到大排序)。很显然要在尽可能多的时间中在权值较大点中休息。所以每次就从当前可能的权值最大的点开始找,并记录下上次休息的点,在这个点一直休息到另一个人追上来,再通过速度差拉开距离,到下一个权值最大的点休息。每次休息的点肯定是越来越靠后的,所以在当前点休息完之后继续向后枚举找到一个比当前点更靠后的权值最大的点,然后开始休息。 设当前点为pos,上次休息的点为last,那么在此处休息对答案的贡献是w[pos]*(pos-last)*(Rf-Rb)。这个贪心的复杂度是O(n)的所以显然能过。值得一提的是给出的L好像并没有什么用。

 

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#define p 1000000007
#define LL long long
using namespace std;
inline LL read()
{
    LL sum=0;
    char ch =getchar();
    while(ch<'0'||ch>'9')
        ch=getchar();
    while(ch>='0'&&ch<='9')
    {
        sum=sum*10+ch-'0';
        ch=getchar();
    }
    return sum;
}
int l,n;
LL rb,rf,ans=0;
struct station
{
    int pos;
    LL w;
}h[100005];
bool cmp(const station &a,const station &b)
{
    if(a.w==b.w) return a.pos<b.pos;
    return a.w>b.w;
}
int main()
{
    l=read();n=read();
    rf=read();rb=read();
    for(register int i=1;i<=n;++i)
    {
        h[i].pos=read();
        h[i].w=read();
    }
    sort(h+1,h+n+1,cmp);
    int maxr=0;
    for(register int i=1;i<=n;++i)
    {
        if(h[i].pos>maxr)
        {
            ans+=h[i].w*(h[i].pos-maxr)*(rf-rb);
            maxr=h[i].pos;
        }
    }
    cout<<ans<<endl;
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值