1033. To Fill or Not to Fill (25)
2013-11-14 13:29
381 查看
1033. To Fill or Not to Fill (25)
该问题是一个贪心算法问题,设满缸油可行驶的距离为s,当前加油站的位置为p1,那么需要找到在s距离内第一个油价比当前加油站油价便宜的点p2,如果s距离内p1油价最便宜,则加满;否则,保证p1位置油缸内的油缸号可以导致p2位置。
该问题是一个贪心算法问题,设满缸油可行驶的距离为s,当前加油站的位置为p1,那么需要找到在s距离内第一个油价比当前加油站油价便宜的点p2,如果s距离内p1油价最便宜,则加满;否则,保证p1位置油缸内的油缸号可以导致p2位置。
#include <vector> #include <iostream> #include <algorithm> using namespace std; class CA { public: class cstation { public: int point,addlen; double price; cstation(){point=0;addlen=0;price=0;} bool operator < (const cstation& st)const { return point<st.point; } }; void run(); int c,d,avg,n; vector<cstation> vst; }; void CA::run() { cin>>c>>d>>avg>>n; cstation st; while(n-->0) { cin>>st.price>>st.point; vst.push_back(st); } sort(vst.begin(),vst.end()); int taglen=c*avg; int i,j,lastlevlen=0,lastpoint=0; vector<int> curlist; bool gotto=false; for(i=0;i<vst.size();i++) { if(vst[i].point-lastpoint>lastlevlen) break; lastlevlen-=vst[i].point-lastpoint; int k=i; for(j=i+1;j<vst.size();j++) //find first sheapest station { if(vst[j].point-vst[i].point<=taglen) { if(vst[j].price<vst[k].price) {k=j;break;} } else break; } if(k==i) vst[i].addlen=taglen-lastlevlen; //current station is sheapest ,fill tank else vst[i].addlen=(vst[k].point-vst[i].point-lastlevlen)>0?(vst[k].point-vst[i].point-lastlevlen):0; //rearch the lastest cheaper station lastpoint=vst[i].point; lastlevlen+=vst[i].addlen; if(vst[i].point+lastlevlen>=d) { vst[i].addlen-=vst[i].point+lastlevlen-d; gotto=true; break; } } if(gotto) { double allprice=0; for(i=0;i<vst.size();i++) { allprice+=vst[i].addlen*vst[i].price/avg; } printf("%.2f",allprice); } else { printf("The maximum travel distance = %.2f",double(lastpoint+lastlevlen)); } } int main() { // freopen("test.in","r",stdin); CA *a=new CA; a->run(); return 0; }
相关文章推荐
- Pull is not possible because you have unmerged ...
- 解决Default storage engine (InnoDB) is not available导致mysql无法启动的修改办法
- Access to SQLserver 数据库生迁脚本编写器(DB_CreateSqlEV1.06下载)
- WMA To MP3 Encoder6.15绿色版含注册文件 下载
- MySQL4 File ‘c:\mysql\share\charsets\?.conf’ not found (Errcode: 22)的解决方法
- C# LINQ to XML应用介绍
- Linq to SQL Delete时遇到问题的解决方法
- How to Auto Include a Javascript File
- Easy RM RMVB to DVD Burner v1.3.8 汉化版 下载 附注册码
- mysql4.1以上版本连接时出现Client does not support authentication protocol问题解决办法
- 解决bash: mysql: command not found 的方法
- php中OR与|| AND与&&的区别总结
- sql not in 与not exists使用中的细微差别
- MYSQL出现" Client does not support authentication "的解决方法
- bash scp command not found的解决方法
- Unable to instantiate default tuplizer [org.hib...
- fedora18 安装virtualbox 步骤 与 解决kernel driver not ...
- ora-00845 MEMORY_TARGET not supported on this system 的问题
- How to create a simple rubics cube
- NSString,NSArray和NSDictionary to JSON标准格式字符串