zoukankan      html  css  js  c++  java
  • Expedition [POJ2431] [贪心]

    题目大意:

    有n个加油站,每个加油站的加油的油量有限,距离终点都有一个距离。

    一个卡车的油箱无限,每走一个单元要消耗一单元的油,问卡车到达终点的最少加多少次油。

    分析:

    我们希望的是走到没油的时候就尽可能加更多的油

    显然我们是没办法随时获得加油的机会的

    但是我们把我们走过的加油站全按从大到小记下来

    没有了就加,如果加油站全都用完了,就代表gg了,puts("-1")

    代码:

     1 #include<queue>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 #define RG register int
     7 #define ll long long
     8 #define maxn 10005
     9 #define rep(i,a,b) for(RG i=a;i<=b;i++)
    10 #define per(i,a,b) for(RG i=a;i>=b;i--)
    11 using namespace std;
    12 int n,f,d,ans,pos,fuel;
    13 int F[maxn],D[maxn];
    14 struct Dat{
    15     int F,D;
    16     bool operator < (const Dat &a)const{
    17         return D<a.D;
    18     }
    19 }dat[maxn];
    20 inline int read()
    21 {
    22     int x=0,f=1;char c=getchar();
    23     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    24     while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    25     return x*f;
    26 }
    27 priority_queue<int> que;
    28 
    29 int main()
    30 {
    31     n=read();
    32     rep(i,1,n)    dat[i].D=read(),dat[i].F=read();
    33     d=read(),f=read();
    34     rep(i,1,n)    dat[i].D=d-dat[i].D;
    35     dat[++n].D=d,dat[n].F=0;
    36     sort(dat+1,dat+1+n);
    37     fuel=f;
    38     rep(i,1,n)
    39     {
    40         int dis=dat[i].D-pos;
    41         while(dis>fuel)
    42         {
    43             if(que.empty())
    44             {
    45                 puts("-1");return 0;
    46             }
    47             ans++;
    48             fuel+=que.top();que.pop();
    49         }
    50         fuel-=dis;
    51         pos=dat[i].D;
    52         que.push(dat[i].F);
    53     }
    54     cout<<ans;
    55     return 0;
    56 }
    View Code
  • 相关阅读:
    HTML2
    HTML1
    MySQL进阶part4
    pymysql模块
    MySQL进阶part3
    MySQL进阶part2
    MySQL进阶part1
    java IO中的乱码问题
    解决在IDEA中无法使用Scanner输入的问题
    IDEA配置xml文件头报错:URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
  • 原文地址:https://www.cnblogs.com/ibilllee/p/9220895.html
Copyright © 2011-2022 走看看