首页 > 其他分享 >POJ--2431 Expedition(优先队列)

POJ--2431 Expedition(优先队列)

时间:2023-01-26 00:33:49浏览次数:62  
标签:town tank Expedition -- 2431 ++ int que fuel

记录
0:17 2023-1-26

http://poj.org/problem?id=2431

reference:《挑战程序设计竞赛(第2版)》2.2.4 p77

Description

A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to run over a rock and puncture the truck's fuel tank. The truck now leaks one unit of fuel every unit of distance it travels.

To repair the truck, the cows need to drive to the nearest town (no more than 1,000,000 units distant) down a long, winding road. On this road, between the town and the current location of the truck, there are N (1 <= N <= 10,000) fuel stops where the cows can stop to acquire additional fuel (1..100 units at each stop).

The jungle is a dangerous place for humans and is especially dangerous for cows. Therefore, the cows want to make the minimum possible number of stops for fuel on the way to the town. Fortunately, the capacity of the fuel tank on their truck is so large that there is effectively no limit to the amount of fuel it can hold. The truck is currently L units away from the town and has P units of fuel (1 <= P <= 1,000,000).

Determine the minimum number of stops needed to reach the town, or if the cows cannot reach the town at all.

Input

  • Line 1: A single integer, N

  • Lines 2..N+1: Each line contains two space-separated integers describing a fuel stop: The first integer is the distance from the town to the stop; the second is the amount of fuel available at that stop.

  • Line N+2: Two space-separated integers, L and P

Output

  • Line 1: A single integer giving the minimum number of fuel stops necessary to reach the town. If it is not possible to reach the town, output -1.

Sample Input

4
4 4
5 2
11 5
15 10
25 10

Sample Output

2

优先队列,当没有油的时候优先选择可以用的最大的加油。这题要注意先做下排序处理,输入估计有未排序的。

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define MAX_N 10000

const int INF = 100000000;

int L, P, N;
int A[MAX_N + 1], B[MAX_N + 1];
typedef struct {
    int A, B;
} T;

T t[MAX_N + 1];

void solve() {
    A[N] = L;
    B[N] = 0;

    N++;
    priority_queue<int> que;

    int ans = 0, pos = 0, tank = P;

    for(int i = 0; i < N; i++) {
        int d = A[i] - pos;

        while (tank - d < 0) {
            if(que.empty()) {
                puts("-1");
                return;
            }
            tank += que.top();
            que.pop();
            ans++;
        }
        
        tank -= d;
        pos = A[i];
        que.push(B[i]);
    }

    printf("%d\n", ans);
}

bool compareT(const T &t1, const T &t2) {
    return t1.A < t2.A;
}

void solve1() {

    t[N].A = L;
    t[N].B = 0;
    // sort(t, t + N + 1, [](const T &t1, const T &t2){ return t1.A < t2.A;});
    sort(t, t + N + 1, compareT);

    N++;
    priority_queue<int> que;

    int ans = 0, pos = 0, tank = P;

    for(int i = 0; i < N; i++) {
        int d = t[i].A - pos;

        while (tank - d < 0) {
            if(que.empty()) {
                puts("-1");
                return;
            }
            tank += que.top();
            que.pop();
            ans++;
        }
        
        tank -= d;
        pos = t[i].A;
        que.push(t[i].B);
    }

    printf("%d\n", ans);
}

int main() {
    scanf("%d", &N);
    for(int i = 0; i < N; i++) {
        scanf("%d %d", &t[i].A, &t[i].B);
    }
    scanf("%d %d", &L, &P);
    for(int i = 0; i < N; i++) {
        t[i].A = L - t[i].A;
    }
    solve1();
}

标签:town,tank,Expedition,--,2431,++,int,que,fuel
From: https://www.cnblogs.com/57one/p/17067490.html

相关文章

  • 百度地图提示JS API版本过低
    当网站遇到调用百度地图页面时,提示“JSAPI版本过低”jsapi版本过低解决方法:在https://lbsyun.baidu.com/注册用户,在应用管理,创建应用应用类型:浏览器端(推荐全......
  • JS_12_表单校验
    实现了常见的注册表单的校验。  初始界面:  同意协议后可以点击注册按钮:  输入符合条件的数据:   实现代码:<!DOCTYPEhtml><html><head>......
  • 【Javaweb】Servlet八 | 请求转发的代码实现【详解】
    请求的转发什么是请求的转发?请求转发是指,服务器收到请求时,从一次资源转到另一个资源的操作叫做请求转发。 部分代码//获取请求的参数(办事的材料)查看......
  • sqlserver事务日志太大的原因
    数据库的日志文件sqlserver的数据库日志文件是事务日志文件,记录了所有数据库的事务和修改,包含在数据库上的增、删、改操作信息。如果日志文件满了,会报9002错误,且数据库只......
  • JavaScript:判断数组对象值是否相同的函数声明
    varobj1={name:"w",};varobj2={name:"w",};functionisObjectValueEqual(a,b){//判断两个对......
  • Blazor Pdf Reader PDF阅读器 组件 更新
    BlazorPdfReaderPDF阅读器组件https://www.nuget.org/packages/BootstrapBlazor.PdfReader#readme-body-tab示例:https://www.blazor.zone/PdfReadershttps://bla......
  • 浙大“python->机器语言“的学习一(做计算)
    T1输入格式本题无输入输出格式输出由3行星号*组成的方形,如样例所示。每行除了最后一个星号,每个星号后面有空格*********是这样的,发现我一行一行输出根......
  • 61Linux命令退格键变成^H的解决办法
    执行py脚本,空格变成^HLinux命令退格键变成^H的解决办法1.按住ctrl键再去按退格键(backspace),就ok了;2.把sttyerase^H添加到.bash_profile中。操作如下:......
  • 进入目标机后应及时升级shell
    在以普通用户进入目标机后应该及时升级shell 命令python-c'importpty;pty.spawn("/bin/bash")' 不然有些命令无法使用像su命令,会返回“mustberunfromter......
  • Openstack-创建实例错误
    创建实例错误实例执行所请求操作失败,实例处于错误状态。:请稍后再试[错误:Buildofinstancebeaeb5e0-26eb-4044-ae14-bb87d509886daborted:Failedtoallocateth......