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 ...
随机推荐
- jquery操作HTML5 的data-*的用法实例分享
.mm{width:256px; height:200px;} .mm[data-name='张含韵']{background:url(http://image.zhangxinxu.com/imag ...
- 图片特效-仿 iPhone 图片查看器效果
—————————————————————— <script type="text/javascript"> var arr = ...
- Codeforce#354_B_Pyramid of Glasses(模拟)
题目连接:http://codeforces.com/contest/676/problem/B 题意:给你一个N层的杯子堆成的金字塔,倒k个杯子的酒,问倒完后有多少个杯子的酒是满的 题解:由于数据不 ...
- Office Web Apps 错误
owa安装完后,在sharepoint网站上打开word,excel,报错,Sorry, something went wrong, 通过correlation id找到错误信息(如何在sharepo ...
- 求最大公约数(GCD)的两种算法
之前一直只知道欧几里得辗转相除法,今天学习了一下另外一种.在处理大数时更优秀的算法--Stein 特此记载 1.欧几里得(Euclid)算法 又称辗转相除法,依据定理gcd(a,b)=gcd(b,a% ...
- 远程连接(ssh安装)
更新源列表打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入当前登录用户的管理员密码"--& ...
- 如何创建自定义ASP.NET MVC5脚手架模板?
I'm using ASP.NET MVC5 and VS2013 I've tried to copy CodeTemplates folder from C:\Program Files (x86 ...
- fdisk添加磁盘
1. 通过Fdisk查看系统分区详细信息: Fdisk –l 详解: [root@jetsenLin ~]# fdisk -l Disk /dev/sda: 10.7 GB, ...
- 省市便利 UIPicherView
@property (strong,nonatomic) UIPickerView *pickerV; @property (strong,nonatomic) NSArray *arr; @prop ...
- asp之vbscript函数
'函数Abs(number)'返回绝对值.Array(arglist)'创建一个数组.Asc(string)'返回字符串第一个字符的ANSI码.Atn(number)'返回反正弦值.CBool(exp ...