P3092 [USACO13NOV]No Change G
题目描述
Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 <= K <= 16), each with value in the range 1…100,000,000. FJ would like to make a sequence of N purchases (1 <= N <= 100,000), where the ith purchase costs c(i) units of money (1 <= c(i) <= 10,000). As he makes this sequence of purchases, he can periodically stop and pay, with a single coin, for all the purchases made since his last payment (of course, the single coin he uses must be large enough to pay for all of these). Unfortunately, the vendors at the market are completely out of change, so whenever FJ uses a coin that is larger than the amount of money he owes, he sadly receives no changes in return!
Please compute the maximum amount of money FJ can end up with after making his N purchases in sequence. Output -1 if it is impossible for FJ to make all of his purchases.
约翰到商场购物,他的钱包里有K(1 <= K <= 16)个硬币,面值的范围是1…100,000,000。
约翰想按顺序买 N个物品(1 <= N <= 100,000),第i个物品需要花费c(i)块钱,(1 <= c(i) <= 10,000)。
在依次进行的购买N个物品的过程中,约翰可以随时停下来付款,每次付款只用一个硬币,支付购买的内容是从上一次支付后开始到现在的这些所有物品(前提是该硬币足以支付这些物品的费用)。不幸的是,商场的收银机坏了,如果约翰支付的硬币面值大于所需的费用,他不会得到任何找零。
请计算出在购买完N个物品后,约翰最多剩下多少钱。如果无法完成购买,输出-1
题目分析
以s表示硬币使用状态,dp[s]表示状态s下最多能买到的物品数,sum记录物品价格前缀和
对于每个s,设pre为去掉s中一个硬币 i 得到的上一个状态
对每个pre,在sum数组里查找小于等于sum[dp[pre]]+vi且最大的位置
用所有pre找到的最大位置来更新dp[s]
其中查找二分即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long lt;
lt read()
{
lt f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return f*x;
}
const lt inf=1e12;
const int maxn=200010;
int n,m;
lt val[20],ci[maxn],sum_val;
lt sum[maxn],dp[maxn];
lt ans=inf;
void solve()
{
for(int s=0;s<(1<<m);++s)
{
for(int i=1;i<=m;++i)
{
if(!((1<<i-1)&s)) continue;
int pre=s^(1<<i-1);
if(dp[pre]==n){
dp[s]=n;
break;
}
lt wi=sum[dp[pre]]+val[i];
lt pos=upper_bound(sum+1,sum+1+n,wi)-sum;
dp[s]=max(dp[s],pos-1);
}
if(dp[s]==n)
{
lt tmp=0;
for(int i=1;i<=m;++i)
if((1<<i-1)&s) tmp+=val[i];
ans=min(ans,tmp);
}
}
}
int main()
{
m=read(); n=read();
for(int i=1;i<=m;++i)
{
val[i]=read();
sum_val+=val[i];
}
for(int i=1;i<=n;++i)
{
ci[i]=read();
sum[i]=sum[i-1]+ci[i];
}
solve();
if(ans==inf) printf("-1");
else printf("%lld",sum_val-ans);
return 0;
}
P4329 [COCI2006-2007#1] Bond【状压DP】
题目大意(from洛谷翻译)
有n个人去执行n个任务,每个人执行每个任务有不同的成功率,每个人只能执行一个任务,求所有任务都执行的总的成功率。
输入第一行,一个整数n(1≤n≤20 ),表示人数兼任务数。接下来n行每行n个数,第i行第j个数表示第i个人去执行第j个任务的成功率
输出最大的总成功率
题目分析
和上题类似,s表示执行任务的人的状态,k表示状态s已安排任务的人数(即二进制下1的个数)
dp[s]表示该状态下完成前k个任务的最大成功率
同样找到s的所有pre,此过程也就相当于枚举了由哪个人去做任务k
找到最大的dp[pre]*rate[i][k]更新dp[s]即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef double db;
int read()
{
int f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return f*x;
}
const db eps=1e-9;
const int maxn=23;
int n,m;
int cnt[1<<maxn];
db rate[maxn][maxn],dp[1<<maxn];
void DP(int s)
{
if(fabs(dp[s])>eps||s==0) return;
for(int i=1;i<=n;++i)
{
if(!((1<<i-1)&s)) continue;
int pre=s^(1<<i-1);
DP(pre);
dp[s]=max(dp[s],dp[pre]*rate[i][cnt[s]]);
}
}
int qnum(int x)
{
int res=0;
while(x)
{
x&=(x-1);
res++;
}
return res;
}
int main()
{
n=read();
for(int i=1;i<=n;++i)
for(int j=1;j<=n;++j)
{
scanf("%lf",&rate[i][j]);
rate[i][j]/=100.0;
}
for(int i=0;i<1<<n;++i)
cnt[i]=qnum(i);
dp[0]=1.0;
DP((1<<n)-1);
printf("%lf",dp[(1<<n)-1]*100.0);
return 0;
}