zoukankan      html  css  js  c++  java
  • 【贪心】 Expedition

    传送门

    题意

    需要驾车到距离(L)的城镇,最开始有(P)单位汽油,每行驶1单位距离消耗1单位汽油,如果汽油耗尽则无法行进,路上一共有(N)个加油站,第(i)个加油站距离城镇(d_{i})个单位,可以加油(b_{i})单位,每个加油站只能加一次,容量无限,求最少的加油次数抵达城镇,如果不能抵达输出-1

    数据范围

    (1 leq N leq 10000)
    (1 leq L leq 1000000 , 1leq P leq 1000000)
    (1 leq d_{i} < L , 1leq b_{i} leq 100)

    题解

    将所有的距离转化一下,加油的时间是在无法抵达当前加油站的时候进行,每次加都贪心的加已经经过的加的最多的

    Code

    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<cstdlib>
    #include<ctime>
    #include<cctype>
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<list>
    #include<queue>
    #include<string>
    #include<set>
    #include<map>
    using namespace std;
    #define rep(i,a,n) for(int i=a;i<n;i++)
    #define per(i,a,n) for(int i=n-1;i>=a;i--)
    #define fi first
    #define se second 
    #define ll long long
    #define pb push_back
    typedef pair<long long,long long> pll;
    typedef pair<int,int> pii;
    typedef vector<int> vi;
    typedef vector<long long> vll;
    typedef double db;
    const ll mod=1e9+7;
    ll powmod(ll a,ll b,ll p){ll res=1;a%=p;while(b){if(b&1) res=res*a%p;a=a*a%p;b>>=1;}return res;}
    ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
    const int N=1e4+10;
    int n;
    int L,P;
    pii sta[N];
    
    int main(){
        scanf("%d",&n);
        rep(i,0,n) 
            scanf("%d%d",&sta[i].fi,&sta[i].se);
        scanf("%d%d",&L,&P);
        rep(i,0,n) 
            sta[i].fi=L-sta[i].fi;
    
        sort(sta,sta+n);
        priority_queue<int>q;
        int ans=0;
        sta[n]=make_pair(L,0);
        n++;
        rep(i,0,n){
            int distance=sta[i].fi,add=sta[i].se;
            while(P < distance){
                if(q.empty()){
                    puts("-1");return 0;
                }
                P += q.top();
                q.pop();
                ans++;
            }
            q.push(add);
        }
        printf("%d
    ",ans);
    }
    
    
  • 相关阅读:
    Stream流之三级查询
    SpringBoot日期格式的设置
    el表达式
    SpringMV+HuTool之验证码登录
    Spring注解详解
    @ResponseBody注解使用(返回字符串并不跳转)
    每日leetcode-数组-589. N 叉树的前序遍历
    python apply函数
    剑指offer-JZ6 旋转数组的最小数字
    torch.manual_seed()函数
  • 原文地址:https://www.cnblogs.com/hhyx/p/13205228.html
Copyright © 2011-2022 走看看