二进制枚举
这次补题目收货比较大的一题
枚举所有状态,对每一个状态找符合要求的最大值
第二个循环是找在这种状态下选了几个
//D
#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct node{
ll peo;
ll max;
};
node home[10];
int main()
{
int t;
ll sum;
scanf("%d",&t);
while(t--)
{
memset(dp,0,sizeof(dp));
scanf("%lld",&sum);
ll nx=0,nm=0;
int cnt=0;
int ans=0;
for(int i=0;i<7;i++)
{
scanf("%lld %lld",&home[i].peo,&home[i].max);
}
for(int i=0;i<(1<<7);i++)//枚举128种状态
{
cnt=0; nx=0; nm=0;
for(int j=0;j<7;j++)
{
if(i&(1<<j))//枚举的状态中选或不选
{
cnt++;
nx+=home[j].peo;
nm+=home[j].max;
}
}
if(nx<=sum&&nm>=sum) ans = max(cnt,ans);
}
printf("%d\n",ans);
}
return 0;
}