来源:HDU5887
背包,但是主要因为数据太大,所以。。。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define fff (it->first)
#define sss (it->second)
using namespace std;
typedef long long LL;
LL n,timl;
const int MAXN = 110;
LL t[MAXN],cost[MAXN];
map<LL,LL>mp;
int main(){
while(scanf("%lld%lld",&n,&timl)!=EOF){
mp.clear();
mp[0]=0;;
for(int i=1;i<=n;i++) {
scanf("%lld%lld",&t[i],&cost[i]);
}
for(int i=1;i<=n;i++) {
LL tt = t[i];
LL cos = cost[i];
map<LL,LL>::reverse_iterator it0;
for(it0 = mp.rbegin();it0!=mp.rend();it0++){
LL ff = it0->first;
if(mp[ff+tt])
mp[ff+tt] = max(mp[ff]+cos,mp[ff+tt]);
else
mp[ff+tt] = mp[ff] + cos;
}
map<LL,LL>::iterator it;
LL maxn = 0;
for(it = mp.begin();it!=mp.end();) {
if(it == mp.begin()) {
it ++;
continue;
}
if(fff>timl) {
map<LL,LL>::iterator itt = it;
it ++ ;
mp.erase(itt);
continue;
}
if(maxn>=sss){
map<LL,LL>::iterator itt = it;
it ++;
mp.erase(itt);
}
else{
maxn = sss;
it++;
}
}
}
LL ans = -1;
map<LL,LL>::iterator it;
for(it=mp.begin();it!=mp.end();++it) {
ans = max(ans,sss);
}
printf("%lld\n",ans);
}
return 0;
}