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

题解

朴素想法就是dfs 经过该点的时候决定是否加油 中间加了一点剪枝 如果加油次数已经比已知最少的加油次数要大或者等于了 那么就剪枝

然而 还是TLE了

TLE代码

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue> using namespace std; vector<pair<int, int>> dis_fuel(); int n;
int L, P; int ret = ; void dfs(int idx,int currAddCount)
{
if (idx == n) {
ret = min(ret, currAddCount);
return;
} if (currAddCount >= ret) return; if (P + dis_fuel[idx].first >= L) {
//当前的油 可以到达 那么选择加油不加油 P += dis_fuel[idx].second;
dfs(idx+, currAddCount+); P -= dis_fuel[idx].second;
dfs(idx + , currAddCount);
} return;
} int solve()
{
cin >> n; for (int i = ; i < n; i++) {
cin >> dis_fuel[i].first >> dis_fuel[i].second;
}
cin >> L >> P; sort(dis_fuel.begin(), dis_fuel.begin() + n, greater<pair<int, int>>()); dfs(,); if (ret == ) ret = -;
return ret;
} int main()
{
cout << solve() << endl; return ;
}

TLE!!!

考虑下经过一个加油站就是增加了一个加油的机会 那么我们尝试不加油继续往前走 走到没有了 看看能经过多少加油站 然后从里面选择加油最多的那个机会 加油 再继续前行

这是很显然的贪心策略 既然要加油次数最少 那么在能加油的机会里贪心选择即可.未细加证明 但是AC了 说明思路是正确的

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue> using namespace std; vector<pair<int, int>> dis_fuel(); int n;
int L, P; int solve()
{
cin >> n; for (int i = ; i < n; i++) {
cin >> dis_fuel[i].first >> dis_fuel[i].second;
}
cin >> L >> P; int ret = ;
sort(dis_fuel.begin(), dis_fuel.begin() + n, greater<pair<int, int>>());
priority_queue<int, vector<int>,less<int>> que; for (int i = ; i < dis_fuel.size(); i++) {
if (dis_fuel[i].first + P >= L) {
que.push(dis_fuel[i].second);
}
else {
while (!que.empty() && dis_fuel[i].first + P < L) {
ret++;
P += que.top();
que.pop();
}
if (dis_fuel[i].first + P < L) return -;
que.push(dis_fuel[i].second);
}
} if (P < L) return -;
return ret;
} int main()
{
cout << solve() << endl; return ;
}

poj 2431 Expedition 贪心 优先队列 题解《挑战程序设计竞赛》的更多相关文章

  1. POJ 2431 Expedition (贪心+优先队列)

    题目地址:POJ 2431 将路过的加油站的加油量放到一个优先队列里,每次当油量不够时,就一直加队列里油量最大的直到能够到达下一站为止. 代码例如以下: #include <iostream&g ...

  2. poj 2431 Expedition 贪心+优先队列 很好很好的一道题!!!

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10025   Accepted: 2918 Descr ...

  3. POJ 2431 Expedition (优先队列+贪心)

    题目链接 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. ...

  4. POJ 2431 Expedition(优先队列、贪心)

    题目链接: 传送门 Expedition Time Limit: 1000MS     Memory Limit: 65536K 题目描述 驾驶一辆卡车行驶L单位距离.最开始有P单位的汽油.卡车每开1 ...

  5. poj - 2431 Expedition (优先队列)

    http://poj.org/problem?id=2431 你需要驾驶一辆卡车做一次长途旅行,但是卡车每走一单位就会消耗掉一单位的油,如果没有油就走不了,为了修复卡车,卡车需要被开到距离最近的城镇, ...

  6. poj 2431 Expedition 贪心

    简单的说说思路,如果一开始能够去到目的地那么当然不需要加油,否则肯定选择能够够着的油量最大的加油站加油,,不断重复这个贪心的策略即可. #include <iostream> #inclu ...

  7. POJ 2431 Expedition 贪心 优先级队列

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30702   Accepted: 8457 Descr ...

  8. 【网络流#9】POJ 2135 Farm Tour 最小费用流 - 《挑战程序设计竞赛》例题

    [题意]给出一张无向图,从1开始到n,求两条没有公共边的最短路,使得路程总和最小 每条边的权值设为费用,最大流量设为1,然后就是从源点到汇点流量为2的最小费用流. 因为是规定了流量,新建一个源点和一个 ...

  9. 【网络流#7】POJ 3281 Dining 最大流 - 《挑战程序设计竞赛》例题

    不使用二分图匹配,使用最大流即可,设源点S与汇点T,S->食物->牛->牛->饮料->T,每条边流量为1,因为流过牛的最大流量是1,所以将牛拆成两个点. 前向星,Dini ...

随机推荐

  1. SAP_ECC6_EHP7_IDES安装文档ORACLE11G+WINDOWS2012 R2 问题总结

    SAP_ECC6_EHP7_IDES安装文档ORACLE11G+WINDOWS2012 R2 问题总结 1.注意密码不能带@等特殊符号,否则会报如下错误,因为ORACLE数据是不容许密码带@的.@是一 ...

  2. C lang: Pointer

    Ax_Terminology xa_pointer pointer is the address used to store the variable;A variable (or data obje ...

  3. 记,NSProxy需要实现哪些方法?

    转注出:https://www.cnblogs.com/xiaobajiu/p/10799962.html 使用NSProxy做替身,代理,多继承,本质上都是用它来转发消息给真身. 观察头文件,NSP ...

  4. webpack-dev-server config.js Cannot find module

    Error: Cannot find module,webpack-dev-server --config 报错找不到模块 webpack-dev-server 设置 webpack.config.j ...

  5. 数据库三,exec内置函数

    数据库三,exec内置函数 一.数据库查询与执行顺序 必备知识 查询语句的基本操作 - select - from - where - group by - having - distinct - o ...

  6. Spring学习的第二天

    第二天总共学习了以下内容: spring中的ioc常用注解: 案例使用xml方式和注解方式实现单表的CRUD操作(但还是需要xml配置文件,并不是纯注解的配置): 改造基于注解的Ioc案例,使用纯注解 ...

  7. [译]Vulkan教程(09)窗口表面

    [译]Vulkan教程(09)窗口表面 Since Vulkan is a platform agnostic API, it can not interface directly with the ...

  8. atom 在Ubuntu 18.04 上安装及基本使用

    前记: Atom 是github专门为程序员推出的一个跨平台文本编辑器.具有简洁和直观的图形用户界面,并有很多有趣的特点:支持CSS,HTML,JavaScript等网页编程语言.它支持宏,自动完成分 ...

  9. Mybatis基本类型参数非空判断(异常:There is no getter for property...)

    先看一小段代码 <select id="queryByPhone" parameterType="java.lang.String" resultType ...

  10. Python3 获取系统资源

    cpu disk mem import osimport psutilos.chdir(os.getcwd()) #cpu def get_cpu_info(): cpu_percent=psutil ...