1002. Business (35)

来源:互联网 发布:java 获取时间 编辑:程序博客网 时间:2024/06/04 19:03

感想:动态规划真正的入门题,之前搞过一个d'p的但是不是很懂,现在算是理解了动态规划的本质是什么了

代码里有原来傻傻的我写的DFS 的方法,果断超时了。。。。。

另:STL里的map添加不用insert,[]运算符里已经重载了insert了!!被网上的博客骗得好惨


#include<iostream>#include<vector>#include<map>#include<deque>#include<cstdio>#include<string>#include<algorithm>using namespace std;int N;long MAXDAY=-1,MAXFPOFIT=-1;struct bus{long profit;long Day_take;long DDL; bool operator<(bus &a){return DDL<a.DDL;}}b[60];string isread;map<long,int> m1,m2 ;vector<bus> q;/*//TLE 的 DFS void DFS(int i,long day,long pro){if(pro+b[i].profit>MAXFPOFIT)MAXFPOFIT=pro+b[i].profit;isread[i]='1';if(m.find(isread)==m.end()){m.insert(pair<string ,int>(isread,pro+b[i].profit));for(int j=0;j<N;j++){if(isread[j]=='0'&&(day+b[i].Day_take+b[j].Day_take)<=b[j].DDL)DFS(j,day+b[i].Day_take,pro+b[i].profit);}}isread[i]='0';}*/int main(){int i,j,k;map<long,int>::iterator it;cin>>N;for(i=0;i<N;i++){cin>>b[i].profit>>b[i].Day_take>>b[i].DDL;if(b[i].DDL>MAXDAY)MAXDAY=b[i].DDL;isread+='0';}/*for(i=0;i<N;i++)DFS(i,0,0);*/sort(b,b+N);m2[0]=0;for(i=0;i<N;i++){m1=m2;for(it=m1.begin();it!=m1.end();it++){if(it->first+b[i].Day_take<=b[i].DDL){if(it->second+b[i].profit>m2[it->first+b[i].Day_take]){m2[it->first+b[i].Day_take]=it->second+b[i].profit;if(MAXFPOFIT<m2[it->first+b[i].Day_take])MAXFPOFIT=m2[it->first+b[i].Day_take];}}}} cout<<MAXFPOFIT;}


0 0
原创粉丝点击