ZOJ 3699 Dakar Rally(贪心)
这是一道贪心题,他的贪心思想很容易想明白,我们保证油箱里的油始终是最便宜的我们最后的花费就能是最少的.实现方法就是:比如现在在i点,我们看邮箱满载能最远到达哪里,不妨设最远到达j,(j >= i + 1),注意此处的达到,只是指能够到达这个路段的开头位置,并不是走完这段路...所以我们到达j点会有一个leave剩余,leave >= 0,但是在i 和 j点之间我们只需要到达第一个油价比i点小的路段开头位置,换上新油就可以了,之所以是第一个,是因为题目要求必须从前往后按顺序走,不妨设中间有没有出现这样的加油站这个命题为flag,若找到flag为true,没找到flag为false.
下面开始讨论,如果flag = true,则我们可以不需要加满油,只需要到达油价低的点就可以了
如果flag = false,就会出现两种情况,如果j < n,即未能走完第n个点,我们需要加满油,但是j >= n点时,我们可以不用加满油,既然贪心就是最优,不能走完了以后还有剩下的油.
这个题实话说我错了好多次,hustvj都快被我刷屏了,可能是因为循环不太好控制,我一开始用的for,后来换了while,又看了别人的写法,才纠正过来.正如到油价最低点那段代码,其实可以直接过去,但我还是选择了i++的形式,花样作死玩怕了...还是稳重AC吧.
另外除了直接循环的方式,还可以用单调队列,某些高端玩家用的这种方法,感兴趣的百度一下吧.
还有,最后结果用long long,int会爆WA.代码如下:(不要吐槽风格太像,我也不想,改了很多次结果改的跟人家差不多了)
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 100010
int len[maxn],unit[maxn],price[maxn];
int main()
{
int t,n,all;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&all);
bool flag = true;
for(int i = ; i < n; i++)
{
scanf("%d%d%d",&len[i],&unit[i],&price[i]);
if(len[i] * unit[i] > all)
{
flag = false;
}
}
if(!flag)
{
puts("Impossible");
continue;
}
long long leave = ,ans = ,need = ,i = ,j;
while(i < n)
{
j = i + ;
need = len[i] * unit[i];
while(j < n && price[j]>=price[i] && all - need >= unit[j] * len[j])
{
need += len[j] * unit[j];
++j;
} if(j < n && price[j] >= price[i])///这步讨论别忘了,没有找到也不需要加满的一种情况
{
ans += (all - leave) * price[i];
leave = all - len[i] * unit[i];
i++;
}
else
{
if(leave > need) leave -= need;
else
{
ans += (need - leave) * price[i];
leave = ;
}
i = j;
}
}
printf("%lld\n",ans);
}
return ;
}
ZOJ 3699 Dakar Rally(贪心)的更多相关文章
- ZOJ 3699 Dakar Rally
Dakar Rally Time Limit: 2 Seconds Memory Limit: 65536 KB Description The Dakar Rally is an annu ...
- POJ 1862 & ZOJ 1543 Stripies(贪心 | 优先队列)
题目链接: PKU:http://poj.org/problem?id=1862 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...
- 2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)
Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB DreamGrid has a nonnegative integer n . He ...
- ZOJ 2702 Unrhymable Rhymes 贪心
贪心.能凑成一组就算一组 Unrhymable Rhymes Time Limit: 10 Seconds Memory Limit: 32768 KB Special Judge ...
- ZOJ 3946 Highway Project 贪心+最短路
题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...
- zoj 2921 Stock(贪心)
Optiver sponsored problem. After years of hard work Optiver has developed a mathematical model that ...
- zoj 1375||poj 1230(贪心)
Pass-Muraille Time Limit: 2 Seconds Memory Limit: 65536 KB In modern day magic shows, passing t ...
- POJ - 2349 ZOJ - 1914 Arctic Network 贪心+Kru
Arctic Network The Department of National Defence (DND) wishes to connect several northern outposts ...
- ZOJ 3870 Team Formation 贪心二进制
B - Team Formation Description For an upcoming progr ...
随机推荐
- git 忽略已跟踪的文件
对于未跟踪的文件,可以编辑.gitignore文件进行忽略. 对于已跟踪的文件,编辑.gitignore文件不会起作用,它只针对未被跟踪的文件,也就是你先设置规则,然后添加的新文件符合这些规则的就会被 ...
- List-----Array
1.Definition Arry数组是一种连续储存的List 储存方式:将线性表中的元素一次储存在连续的储存空间中. Computer's logical structure: 逻辑位置上相邻的元素 ...
- LeetCode OJ 189. Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- MT5基础知识
获取账户相关信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 printf("ACCOUNT_BALANCE ...
- Git 怎么创建本地库,向本地库提交文件
创建版本库是非常简单的,首先选择一个自己想放的位置,创建一个空目录: (用windows的git bash,这个工具的操作命令和linux下类似) $ mkdir gitRespository ...
- CATransform3D的使用以及各个参数的含义
1. 缩放 CABasicAnimation *theAnimation=[CABasicAnimation animationWithKeyPath:@"transform"]; ...
- C++中为什么构造函数初始化列表
已经有个构造函数负责初始化,为什么还需要构造函数初始化表呢? 在以下三种情况下需要使用初始化成员列表:一,需要初始化的数据成员是对象的情况:二,需要初始化const修饰的类成员:三,需要初始化引用成员 ...
- listview条目用状态选择器没反应
button和imagebutton天生具有“可点击(click)”.“可按下(press)”的特性,radiobutton具有“可勾选(check)”的特性,但是listview的条目只有“可按下( ...
- 控制器view的加载顺序initWithNibName >>> viewDidLoad >>> viewWillAppear >>> viewDidAppear
-(void)viewWillAppear:(BOOL)animated { self.navigationController.navigationBarHidden=NO;// 邓超界:放在wil ...
- 详细,Qt Creator快捷键大全,附快捷键配置方法
一.快捷键配置方法: 进入“工具->选项->环境->键盘”即可配置快捷键. 二.常用默认快捷键: 编号 快捷键 功能 1 Esc 切换到代码编辑状态 2 F1 查看帮助(选中某一类或 ...