hdu5887 Herbs Gathering
神他妈随便写写就能过…
暴力枚举每个取不取
两个剪纸:
1.当剩下可用的时间小于最少需要用的时间 跳出
2.当剩下的植物按照理想情况(甚至可以取一部分)得到的极限答案比已经求出的答案大 跳出
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
typedef long long ll;
struct Node{
ll a,b;
}p[105];
int cmp(Node x,Node y) {
ll t1 = 1ll*x.b*y.a; ll t2 = 1ll*x.a*y.b;
return t1 > t2;
}
ll N,M;
ll ans;
ll minpay;
void dfs(int x,ll pay,ll gain) {
if(minpay > M-pay || x == N+1) {
ans = max(ans, gain);
return;
}
ll P = pay; ll G = gain;
for(int i = x; i <= N; ++i) {
if(P+p[i].a <= M) P += p[i].a, G += p[i].b;
else { G += (M-P)*p[i].b/p[i].a+1; break; }
}
if(G <= ans) return;
dfs(x+1,pay,gain);
if(pay+p[x].a <= M) dfs(x+1, pay+p[x].a, gain+p[x].b);
}
int main(){
while(~scanf("%lld %lld",&N,&M)) {
minpay = INF;
for(int i = 1; i <= N; ++i) {
scanf("%lld %lld",&p[i].a, &p[i].b);
minpay = min(minpay, p[i].a);
}
sort(p+1,p+N+1,cmp);
ans = 0;
dfs(2,0,0);
if(p[1].a <= M) dfs(2,p[1].a,p[1].b);
printf("%lld\n",ans);
}
return 0;
}