P4266 [USACO18FEB]Rest Stops

本文介绍了一种解决特定问题的贪心算法实现方案。该问题可通过选择当前最优解来解决,并利用优先队列(堆)来高效地进行维护。文章提供了一个C++示例代码,用于计算基于草的位置和美味度的最优解。

一眼过去

什么都没想到
认真分析了一下,觉得可能是个dp或者贪心。
脑玩了一下,大概搞出来个贪心策略,就是直接选当前能选的最优选择就好,证明也很容易,反证即可
还可以顺便证明草的tastiness相同但地点不同时对权值贡献没有影响。
贪心用堆维护就好

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 1e5 + 20;
typedef pair<int, int> P;
typedef long long ll;
inline int read()
{
    int x = 0; char ch = getchar(); bool f = false;
    while(!isdigit(ch)) f |= (ch == '-'), ch = getchar();
    while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
    return f ? -x : x;
}

priority_queue<P> q; 
int L, N, rf, rb;

int main()
{
    cin>>L>>N>>rf>>rb;
    for(int i = 1; i <= N; i++){
        P pos;
        pos.second = read(), pos.first = read();
        q.push(pos);
    }

    int pb = 0; ll ans = 0;
    while(!q.empty()){
        P pos = q.top(); q.pop();
        if(pos.second <= pb) continue;
        ans += (1LL * rf - rb) * (1LL * pos.second - pb) * pos.first;
        pb = pos.second;
    }
    cout<<ans<<endl;
    return 0;
}

转载于:https://www.cnblogs.com/wsmrxc/p/9419798.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值