不设找零 No Change (nochange) usaco2013 题解

题目描述

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.

FJ正在市场上为他的农场采购日用品,他口袋里有K(1<=K<=16)个硬币,每一个硬币的面值均在1~1,00,000,000之间。FJ有一个包含N(1<=N<=100,000)种待购物品的序列清单,其中第i种物品需要的钱数为c(i),(1<= c(i) <=10,000),在购物的过程中,物品必须按照清单顺序购买,他随时可以停下来用一枚硬币付一次钱,每次付钱的对象为从上次付钱之后至当前所有物品价值和(当然,他所付的硬币面值也必须足够大),不巧的是,市场上的商户们都没有零钱了,因此如果FJ给的硬币面值大于所购物品价值,他也不会得到找零!
请计算FJ完成N件物品的购物后,所能剩下的最大钱数。如果他无法买到所有物品,输出-1。

输入

  • Line 1: Two integers, K and N.

  • Lines 2..1+K: Each line contains the amount of money of one of FJ’s coins.

  • Lines 2+K..1+N+K: These N lines contain the costs of FJ’s intended purchases.

第1行:两个整数K,N;
第2~K+1行:每行为一个硬币面值;
第K+2~N+K+1行:这N行为FJ所要购买的N件物品的价值。

输出

  • Line 1: The maximum amount of money FJ can end up with, or -1 if FJ cannot complete all of his purchases.

一行,即结束购物后FJ所剩余的最大钱数,输出-1表示他无法完成购物。

样例输入

3 6
12
15
10
6
3
3
2
3
7

样例输出

12

想法

  • 观察到K<=16很容易想到状压
  • 对于任意状态S 对其进行转移时 新状态可以通过二分得到

算法

  • 设计状态f[S]表示S状态下 可以购买i个物品
  • 转移时枚举使用哪个硬币 f[S]=f[S^(1<<(i-1))]+二分出的答案
  • 最终的答案为f[S]=n 情况中空硬币和的最大值
  • 其中二分的部分其实是可以预处理的(然而我并没有)

- 代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#define MAXN 100005
using namespace std;
int k,n,x,size,cost[20],pos[20],sum[MAXN],f[(1<<16)+11],cnt,ans=-1;
int main()
{
    //freopen("nochange.in","r",stdin);
    //freopen("nochange.out","w",stdout);
    scanf("%d%d",&k,&n);
    size=1<<k;
    for (int i=1;i<=k;i++)
    {
        scanf("%d",&cost[i]);
        pos[i]=1<<(i-1);
    }
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        sum[i]=sum[i-1]+x;
    }
    for (int i=0;i<size;i++)
    {
        if(f[i]==n)
        {
            cnt=0;
            for (int j=1;j<=k;j++)
            {
                if((i&pos[j])==0)cnt+=cost[j];
            }
            ans=max(ans,cnt);
        }
        for (int j=1;j<=k;j++)
        {
            if((i&pos[j]))continue;
            int next=i^pos[j];
            int l=f[i],r=n;
            while(l<r)
            {
                int mid=(l+r+1)>>1;
                if(sum[mid]-sum[f[i]]<=cost[j])l=mid;
                else r=mid-1;
            }
            f[next]=max(f[next],l);
        }
    }
    printf("%d\n",ans);
    return 0;
}

你可能感兴趣的:(usaco2013)