当前位置: 代码迷 >> 综合 >> uva 10280 Old Wine Into New Bottles(完全背包+剪枝)*
  详细解决方案

uva 10280 Old Wine Into New Bottles(完全背包+剪枝)*

热度:50   发布时间:2023-11-15 15:10:48.0

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1221

#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define read(x,y) scanf("%d%d",&x,&y)
#define ll long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
const int  maxn =1e6+5;
const int mod=1e9+7;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*
题目大意:给定几个瓶子(瓶子有最大容量和最小容量),
然后给定酒量,问最少的剩余酒量是多少。直接背包的话会超时,
最差复杂度会高达八次方,
优化方法我也是看了别人的题解才懂的,链接:
https://blog.csdn.net/yan_____/article/details/8671147*/int n,k,a[110],b[110];
int dp[maxn],v,vis[4600];
int cnt,tp[4600];
int main()
{int t;scanf("%d",&t);while(t--){scanf("%d%d",&k,&n);k*=1000;v=(1<<30);for(int i=0;i<n;i++){scanf("%d%d",&a[i],&b[i]);if(v>(a[i]*a[i])/(b[i]-a[i])) v=a[i]*a[i]/(b[i]-a[i]);}if(k>=v){puts("0");if(t)puts("");}else{memset(vis,0,sizeof(vis));cnt=0;memset(dp,0,sizeof(dp));dp[0]=1;for(int i=0;i<n;i++)for(int j=a[i];j<=b[i];j++)if(vis[j]==0){vis[j]=1;tp[cnt++]=j;}for(int i=0;i<cnt;i++)for(int j=tp[i];j<=k;j++)if(dp[j-tp[i]]==1) dp[j]=1;int ans=0; for(int i=k;i>=0;i--) if(dp[i]) {ans=i;break;}printf("%d\n",k-ans);if(t) puts("");}}return 0;
}
/*
100
10 2
4450 4500
725 750
*/