HDU5477 A Sweet Journey 模拟
来源:互联网 发布:数据的分散和集中程度 编辑:程序博客网 时间:2024/06/05 20:42
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5477
题目大意:有一条长度为L的路,上面有沼泽地和平原,单位长度的沼泽地需要花费A能量才能通过,单位长度的平原可以恢复B能量,现在给出你L和n段沼泽地的位置,问开始至少需要有多少能量才能通过这条路。
分析:从0开始出发,能量值ans设为0,如果是平原,变量tmp加上这段平原恢复的能量,遇到沼泽地减去需要的能量,如果tmp值为负,说明前面平原积累的能量不足以通过这块沼泽地,我们开始就需要多积累-tmp的能量,这些能量加到ans上,同时tmp赋值为0.直至通过最后一块沼泽地,则ans即为开始至少需要的能量。
实现代码如下:
#include <cstdio>#include <algorithm>using namespace std;const int N=1e5+10;struct node{ int l,r; bool operator <(const node &a) const { return l<a.l; }}inter[N];int main(){ int t,T=1,n,A,B,L; scanf("%d",&t); while(t--) { scanf("%d%d%d%d",&n,&A,&B,&L); for(int i=1;i<=n;i++) scanf("%d%d",&inter[i].l,&inter[i].r); inter[0].l=inter[0].r=0; sort(inter,inter+n+1); int ans=0,tmp=0; for(int i=1;i<=n;i++) { tmp+=(inter[i].l-inter[i-1].r)*B; tmp-=(inter[i].r-inter[i].l)*A; if(tmp<0) { ans=ans-tmp; tmp=0; } } printf("Case #%d: %d\n",T++,ans); } return 0;}
0 0
- HDU5477 A Sweet Journey 模拟
- hdu5477-模拟-A Sweet Journey
- hdu5477 A Sweet Journey(简单模拟)
- hdu5477 A Sweet Journey
- HDU5477 A Sweet Journey
- hdu5477 A Sweet Journey(水)
- HDOJ5477 A Sweet Journey(模拟)
- hdu 5477 A Sweet Journey 模拟
- A Sweet Journey
- A Sweet Journey
- A Sweet Journey
- A Sweet Journey 【二分】
- ACM--过沼泽--模拟--HDOJ 5477--A Sweet Journey
- HDU 5477: A Sweet Journey
- hdu 5477 A Sweet Journey
- HDU 5477 A Sweet Journey
- HDU 5477 A Sweet Journey
- HDU 5477 A Sweet Journey
- the use of github in the Ubuntu
- myeclipse 2015 stable2.0 激活
- Hibernate 一级缓存
- 使用Word2013/2016发布CSDN博客
- USACO刷怪塔的开始
- HDU5477 A Sweet Journey 模拟
- 【猪猪-前端】HTML5+CSS3技术制作的计算器,下载即可使用,学习HTML5必备DEMO
- 美國公佈 防老年癡呆幾個秘訣: "飲食寡淡是老年癡呆的禍根"
- Light oj 1180 - Software Company(二分+背包)
- URLSession 介绍
- JFreeChart(三.饼状图.2)
- 这件事如何引发李连杰、高晓松、吴莫愁共同参与?
- Prime Ring Problem 1016 (dfs)
- iOS程序的启动过程